12. Virtual Environments and Packages虚拟环境和软件包

12.1. Introduction介绍

Python applications will often use packages and modules that don’t come as part of the standard library. Python应用程序通常会使用标准库中没有的包和模块。Applications will sometimes need a specific version of a library, because the application may require that a particular bug has been fixed or the application may be written using an obsolete version of the library’s interface.应用程序有时需要特定版本的库,因为应用程序可能需要修复特定的错误,或者应用程序可能使用库接口的过时版本编写。

This means it may not be possible for one Python installation to meet the requirements of every application. 这意味着一个Python安装可能无法满足每个应用程序的要求。If application A needs version 1.0 of a particular module but application B needs version 2.0, then the requirements are in conflict and installing either version 1.0 or 2.0 will leave one application unable to run.如果应用程序A需要特定模块的1.0版,而应用程序B需要2.0版,则要求存在冲突,安装1.0版或2.0版将导致一个应用程序无法运行。

The solution for this problem is to create a virtual environment, a self-contained directory tree that contains a Python installation for a particular version of Python, plus a number of additional packages.这个问题的解决方案是创建一个虚拟环境,一个自包含的目录树,其中包含针对特定版本Python的Python安装,以及一些附加包。

Different applications can then use different virtual environments. 然后,不同的应用程序可以使用不同的虚拟环境。To resolve the earlier example of conflicting requirements, application A can have its own virtual environment with version 1.0 installed while application B has another virtual environment with version 2.0. 为了解决前面的冲突需求示例,应用程序A可以安装自己的1.0版虚拟环境,而应用程序B可以安装另一个2.0版虚拟环境。If application B requires a library be upgraded to version 3.0, this will not affect application A’s environment.如果应用程序B要求将库升级到3.0版,这不会影响应用程序a的环境。

12.2. Creating Virtual Environments创建虚拟环境

The module used to create and manage virtual environments is called venv. 用于创建和管理虚拟环境的模块称为venvvenv will usually install the most recent version of Python that you have available. venv通常会安装最新版本的Python。If you have multiple versions of Python on your system, you can select a specific Python version by running python3 or whichever version you want.如果系统上有多个Python版本,可以通过运行python3或任何想要的版本来选择特定的Python版本。

To create a virtual environment, decide upon a directory where you want to place it, and run the venv module as a script with the directory path:要创建虚拟环境,请确定要放置虚拟环境的目录,并以脚本的形式运行venv模块,其中包含以下目录路径:

python3 -m venv tutorial-env

This will create the tutorial-env directory if it doesn’t exist, and also create directories inside it containing a copy of the Python interpreter and various supporting files.这将创建tutorial-env目录(如果它不存在),并在其中创建包含Python解释器副本和各种支持文件的目录。

A common directory location for a virtual environment is .venv. 虚拟环境的一个常见目录位置是.venvThis name keeps the directory typically hidden in your shell and thus out of the way while giving it a name that explains why the directory exists. 这个名称通常会将目录隐藏在shell中,因此不会对其造成影响,同时为其提供一个解释目录存在原因的名称。It also prevents clashing with .env environment variable definition files that some tooling supports.它还可以防止与某些工具支持的.env环境变量定义文件发生冲突。

Once you’ve created a virtual environment, you may activate it.一旦创建了虚拟环境,就可以激活它。

On Windows, run:在Windows上,运行:

tutorial-env\Scripts\activate.bat

On Unix or MacOS, run:在Unix或MacOS上,运行:

source tutorial-env/bin/activate

(This script is written for the bash shell. (此脚本是为bash shell编写的。If you use the csh or fish shells, there are alternate activate.csh and activate.fish scripts you should use instead.)如果使用cshfish shell,则应该使用其他activate.cshactivate.fish脚本。)

Activating the virtual environment will change your shell’s prompt to show what virtual environment you’re using, and modify the environment so that running python will get you that particular version and installation of Python. 激活虚拟环境将更改shell的提示,以显示您正在使用的虚拟环境,并修改环境,以便运行python将获得特定版本和安装的python。For example:例如:

$ source ~/envs/tutorial-env/bin/activate
(tutorial-env) $ python
Python 3.5.1 (default, May 6 2016, 10:59:36)
...
>>> import sys
>>> sys.path
['', '/usr/local/lib/python35.zip', ...,
'~/envs/tutorial-env/lib/python3.5/site-packages']
>>>

12.3. Managing Packages with pip使用pip管理包

You can install, upgrade, and remove packages using a program called pip. 您可以使用名为pip的程序安装、升级和删除软件包。By default pip will install packages from the Python Package Index, <https://pypi.org>. 默认情况下,pip将从Python包索引安装包<https://pypi.org>安装包。You can browse the Python Package Index by going to it in your web browser.您可以在web浏览器中浏览Python包索引。

pip has a number of subcommands: “install”, “uninstall”, “freeze”, etc. 有许多子命令:“install”、“uninstall”、“freeze”等。(Consult the Installing Python Modules guide for complete documentation for pip.)(有关pip的完整文档,请参阅《安装Python模块指南》)

You can install the latest version of a package by specifying a package’s name:通过指定软件包的名称,可以安装软件包的最新版本:

(tutorial-env) $ python -m pip install novas
Collecting novas
Downloading novas-3.1.1.3.tar.gz (136kB)
Installing collected packages: novas
Running setup.py install for novas
Successfully installed novas-3.1.1.3

You can also install a specific version of a package by giving the package name followed by == and the version number:您还可以通过提供软件包名称,后跟==和版本号来安装软件包的特定版本:

(tutorial-env) $ python -m pip install requests==2.6.0
Collecting requests==2.6.0
Using cached requests-2.6.0-py2.py3-none-any.whl
Installing collected packages: requests
Successfully installed requests-2.6.0

If you re-run this command, pip will notice that the requested version is already installed and do nothing. 如果重新运行此命令,pip将注意到请求的版本已经安装,并且不执行任何操作。You can supply a different version number to get that version, or you can run pip install --upgrade to upgrade the package to the latest version:您可以提供不同的版本号以获得该版本,也可以运行pip install --upgrade将软件包升级到最新版本:

(tutorial-env) $ python -m pip install --upgrade requests
Collecting requests
Installing collected packages: requests
Found existing installation: requests 2.6.0
Uninstalling requests-2.6.0:
Successfully uninstalled requests-2.6.0
Successfully installed requests-2.7.0

pip uninstall followed by one or more package names will remove the packages from the virtual environment.后跟一个或多个包名将从虚拟环境中删除这些包。

pip show will display information about a particular package:将显示有关特定程序包的信息:

(tutorial-env) $ pip show requests
---
Metadata-Version: 2.0
Name: requests
Version: 2.7.0
Summary: Python HTTP for Humans.
Home-page: http://python-requests.org
Author: Kenneth Reitz
Author-email: me@kennethreitz.com
License: Apache 2.0
Location: /Users/akuchling/envs/tutorial-env/lib/python3.4/site-packages
Requires:

pip list will display all of the packages installed in the virtual environment:将显示虚拟环境中安装的所有软件包:

(tutorial-env) $ pip list
novas (3.1.1.3)
numpy (1.9.2)
pip (7.0.3)
requests (2.7.0)
setuptools (16.0)

pip freeze will produce a similar list of the installed packages, but the output uses the format that pip install expects. 将生成一个类似的已安装软件包列表,但输出使用pip install期望的格式。A common convention is to put this list in a requirements.txt file:常见的惯例是将此列表放入requirements.txt文件中:

(tutorial-env) $ pip freeze > requirements.txt
(tutorial-env) $ cat requirements.txt
novas==3.1.1.3
numpy==1.9.2
requests==2.7.0

The requirements.txt can then be committed to version control and shipped as part of an application. 然后,requirements.txt可以提交到版本控制中,并作为应用程序的一部分发布。Users can then install all the necessary packages with install -r:然后,用户可以使用install -r安装所有必要的软件包:

(tutorial-env) $ python -m pip install -r requirements.txt
Collecting novas==3.1.1.3 (from -r requirements.txt (line 1))
...
Collecting numpy==1.9.2 (from -r requirements.txt (line 2))
...
Collecting requests==2.7.0 (from -r requirements.txt (line 3))
...
Installing collected packages: novas, numpy, requests
Running setup.py install for novas
Successfully installed novas-3.1.1.3 numpy-1.9.2 requests-2.7.0

pip has many more options. 有更多的选择。Consult the Installing Python Modules guide for complete documentation for pip. 有关pip的完整文档,请参阅安装Python模块指南When you’ve written a package and want to make it available on the Python Package Index, consult the Distributing Python Modules guide.当您编写了一个包,并希望在Python包索引中提供它时,请参阅分发Python模块指南