博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python示例_示例介绍Python虚拟环境
阅读量:2523 次
发布时间:2019-05-11

本文共 4738 字,大约阅读时间需要 15 分钟。

python示例

Virtual environments can be described as isolated installation directories. This isolation allows you to localized the installation of your project’s dependencies, without forcing you to install them system-wide.

虚拟环境可以描述为隔离的安装目录。 这种隔离使您可以本地化项目依赖项的安装,而不必强制您在系统范围内安装它们。

Imagine you have two applications, App1 and App2. Both use the package Pak, but require different versions. If you install Pak version 2.3 for App1, you would not be able to run App2 because it requires version 3.1.

假设您有两个应用程序,App1和App2。 两者都使用Pak软件包,但是需要不同的版本。 如果为App1安装Pak 2.3版,则将无法运行App2,因为它需要版本3.1。

This is where virtual environments come in handy.

这是虚拟环境派上用场的地方。

Benefits:

好处:

  • You can have multiple environments, with multiple sets of packages, without conflicts among them. This way, different projects’ requirements can be satisfied at the same time.

    您可以具有多个环境,并具有多组软件包,而彼此之间没有冲突。 这样,可以同时满足不同项目的需求。
  • You can easily release your project with its own dependent modules.

    您可以使用自己的依赖模块轻松发布项目。

Here are two ways you can create Python virtual environments.

这是创建Python虚拟环境的两种方法。

虚拟环境 (Virtualenv)

is a tool used to create isolated Python environments. It creates a folder which contains all the necessary executables to use the packages that a Python project would need.

是用于创建隔离的Python环境的工具。 它创建一个文件夹,其中包含使用Python项目所需的软件包所需的所有必需可执行文件。

You can install it with pip:

您可以使用pip安装它:

pip install virtualenv

Verify the installation with the following command:

使用以下命令验证安装:

virtualenv --version

创建环境 (Create an Environment)

To create a virtual environment use:

要创建虚拟环境,请使用:

virtualenv --no-site-packages my-env

This creates a folder in the current directory with the name of the environment (my-env/). This folder contains the directories for installing modules and Python executables.

这将在当前目录中使用环境名称( my-env/ )创建一个文件夹。 该文件夹包含用于安装模块和Python可执行文件的目录。

You can also specify the Python version you want to work with. Just use the argument --python=/path/to/python/version. For instance, python2.7:

您还可以指定要使用的Python版本。 只需使用参数--python=/path/to/python/version 。 例如python2.7

virtualenv --python=/usr/bin/python2.7 my-env

列出环境 (List Environments)

You can list the available environments with:

您可以通过以下方式列出可用的环境:

lsvirtualenv

激活环境 (Activate an Environment)

Before you can start using the environment you need to activate it:

在开始使用环境之前,需要先激活它:

source my-env/bin/activate

This ensures that only packages under my-env/ are used.

这样可以确保仅使用my-env/下的软件包。

You will notice that the name of the environment is shown on the left of the prompt. This way you can see which is the active environment.

您会注意到,环境名称显示在提示的左侧。 这样,您可以看到哪个是活动环境。

安装套件 (Install Packages)

You can install packages one by one, or by setting a requirements.txt file for your project.

您可以一个接一个地安装软件包,也可以通过为项目设置一个requirements.txt文件来安装。

pip install some-packagepip install -r requirements.txt

If you want to create a requirements.txt file from the already installed packages, run the following command:

如果要从已安装的程序包中创建requirements.txt文件,请运行以下命令:

pip freeze > requirements.txt

The file will contain the list of all the packages installed in the current environment, and their respective versions. This will help you release your project with its own dependent modules.

该文件将包含当前环境中安装的所有软件包及其各自版本的列表。 这将帮助您使用自己的依赖模块发布项目。

停用环境 (Deactivate an Environment)

If you are done working with the virtual environment you can deactivate it with:

如果您已经完成了虚拟环境的工作,则可以通过以下方式停用它:

deactivate

This puts you back to the system’s default Python interpreter with all its installed libraries.

这使您返回系统的默认Python解释器及其所有已安装的库。

删除环境 (Delete an Environment)

Simply delete the environment folder.

只需删除环境文件夹。

conda (Conda)

is a package, dependency and environment management for many languages, including Python.

是许多语言(包括Python)的软件包,依赖项和环境管理。

To install Conda, follow these .

要安装Conda,请按照以下 。

创建环境 (Create an Environment)

To create a virtual environment use:

要创建虚拟环境,请使用:

conda create --name my-env

Conda will create the corresponding folder inside the Conda installation directory.

Conda将在Conda安装目录中创建相应的文件夹。

You can also specify which version of Python you want to work with:

您还可以指定要使用的Python版本:

conda create --name my-env python=3.6

列出环境 (List Environments)

You can list all the available environments with:

您可以使用以下命令列出所有可用的环境:

conda info --envs

激活环境 (Activate an Environment)

Before you can start using the environment you need to activate it:

在开始使用环境之前,需要先激活它:

source activate my-env

安装套件 (Install Packages)

The same as with virtualenv.

virtualenv相同。

停用环境 (Deactivate an Environment)

If you are done working with the virtual environment you can deactivate it with:

如果您已经完成了虚拟环境的工作,则可以使用以下方法将其停用:

source deactivate

删除环境 (Remove an Environment)

If you want to remove an environment from Conda use:

如果要从Conda删除环境,请使用:

conda remove --name my-env

翻译自:

python示例

转载地址:http://oozzd.baihongyu.com/

你可能感兴趣的文章
HDU - 1272 小希的迷宫
查看>>
EntityFramework(1)基础概念与Database First
查看>>
Spring Boot 任务
查看>>
2018APIO 进京赶考
查看>>
Duilib程序添加托盘图标显示
查看>>
在windows上搭建redis集群(redis-cluster)
查看>>
【省选十连测之九】【DP】【组合计数去重】【欧拉函数】基本题
查看>>
文件上传功能 -- jquery.form.js/springmvc
查看>>
阿里云ecs(phpstudy一件包)
查看>>
Python核心编程的四大神兽:迭代器、生成器、闭包以及装饰器
查看>>
linux /proc/sys/fs/file-nr /proc/sys/fs/file-max /etc/security/limits.conf 三者的关联
查看>>
AndroidStudio-快捷键
查看>>
用python DIY一个图片转pdf工具并打包成exe
查看>>
6月14 空控制器和空操作及命名空间
查看>>
volicity文法学习和总结
查看>>
block 块的内部结构
查看>>
IDEA修改git账号密码
查看>>
C# 插入排序
查看>>
每周总结16
查看>>
9_2二维数组
查看>>