本文共 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:
好处:
Here are two ways you can create Python virtual environments.
这是创建Python虚拟环境的两种方法。
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
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
You can list the available environments with:
您可以通过以下方式列出可用的环境:
lsvirtualenv
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.
您会注意到,环境名称显示在提示的左侧。 这样,您可以看到哪个是活动环境。
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.
该文件将包含当前环境中安装的所有软件包及其各自版本的列表。 这将帮助您使用自己的依赖模块发布项目。
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解释器及其所有已安装的库。
Simply delete the environment folder.
只需删除环境文件夹。
is a package, dependency and environment management for many languages, including Python.
是许多语言(包括Python)的软件包,依赖项和环境管理。
To install Conda, follow these .
要安装Conda,请按照以下 。
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
You can list all the available environments with:
您可以使用以下命令列出所有可用的环境:
conda info --envs
Before you can start using the environment you need to activate it:
在开始使用环境之前,需要先激活它:
source activate my-env
The same as with virtualenv
.
与virtualenv
相同。
If you are done working with the virtual environment you can deactivate it with:
如果您已经完成了虚拟环境的工作,则可以使用以下方法将其停用:
source deactivate
If you want to remove an environment from Conda use:
如果要从Conda删除环境,请使用:
conda remove --name my-env
翻译自:
python示例
转载地址:http://oozzd.baihongyu.com/