12. 虚拟环境和包

12.1. 介绍

Python应用程序通常使用不作为标准库一部分的包和模块。应用程序有时需要库的特定版本,因为应用程序可能要求修复特定的bug,或者使用库接口的过时版本编写应用程序。

这意味着一个python安装可能无法满足每个应用程序的要求。如果应用程序A需要特定模块的1.0版,但应用程序B需要2.0版,则这些要求存在冲突,安装1.0版或2.0版将使一个应用程序无法运行。

这个问题的解决方案是创建一个 virtual environment ,一个自包含的目录树,其中包含针对特定版本的python安装,以及一些附加包。

然后,不同的应用程序可以使用不同的虚拟环境。为了解决前面的冲突需求示例,应用程序A可以安装自己的1.0版虚拟环境,而应用程序B可以安装另一个2.0版虚拟环境。如果应用程序B需要将库升级到3.0版,这不会影响应用程序A的环境。

12.2. 创建虚拟环境

用于创建和管理虚拟环境的模块称为 venv . venv 通常安装最新版本的python。如果系统中有多个版本的python,则可以通过运行 python3 或者你想要的版本。

若要创建虚拟环境,请决定要将其放置到的目录,然后运行 venv 模块作为脚本,目录路径为:

python3 -m venv tutorial-env

这将创建 tutorial-env 目录,如果它不存在,还可以在其中创建包含Python解释器副本、标准库和各种支持文件的目录。

虚拟环境的公用目录位置是 .venv .这个名称通常会将目录隐藏在shell中,因此在为其提供一个解释该目录存在的原因的名称时,它会被挡住。它还可以防止与 .env 一些工具支持的环境变量定义文件。

创建虚拟环境后,可以激活它。

在Windows上,运行:

tutorial-env\Scripts\activate.bat

在Unix或MacOS上,运行:

source tutorial-env/bin/activate

(这个脚本是为bash shell编写的。如果您使用 cshfish 炮弹,有备用的 activate.cshactivate.fish 您应该使用的脚本。)

激活虚拟环境将更改shell的提示,以显示正在使用的虚拟环境,并修改环境,以便运行 python 将得到特定版本的python和安装。例如:

$ 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. 使用PIP管理包

可以使用名为 pip . 默认情况下 pip 将从python包索引安装包,<https://pypi.org>。您可以在Web浏览器中浏览python包索引,也可以使用 pip 的有限搜索功能:

(tutorial-env) $ pip search astronomy
skyfield               - Elegant astronomy for Python
gary                   - Galactic astronomy and gravitational dynamics.
novas                  - The United States Naval Observatory NOVAS astronomy library
astroobs               - Provides astronomy ephemeris to plan telescope observations
PyAstronomy            - A collection of astronomy related tools for Python.
...

pip 具有多个子命令:“搜索”、“安装”、“卸载”、“冻结”等(请参阅 安装python模块 完整文件指南 pip

可以通过指定包的名称来安装包的最新版本:

(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

您还可以通过在包名称后面加上 == 版本号:

(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

如果你重新运行这个命令, pip 将注意到请求的版本已安装,不执行任何操作。您可以提供不同的版本号来获取该版本,也可以运行 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 后跟一个或多个包名称将从虚拟环境中删除包。

pip show 将显示有关特定包的信息:

(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 将显示虚拟环境中安装的所有包:

(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 将生成已安装包的类似列表,但输出使用的格式 pip install 期待。一个常见的惯例是将此列表放入 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

这个 requirements.txt 然后可以提交到版本控制并作为应用程序的一部分发送。然后,用户可以使用 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 还有很多选择。查阅 安装python模块 完整文件指南 pip . 当您编写了一个包并希望它在python包索引上可用时,请参考 分发python模块 指南。