17.14. venv — 创建虚拟环境 | 开发者工具 |《python 3 标准库实例教程》| python 技术论坛-380玩彩网官网入口
本节目标:创建隔离的安装和运行环境。
我们可以使用 venv
来管理 python 的虚拟环境,虚拟环境可以用一种隔离环境安装包和运行程序。每个隔离环境都有自己的解释器文件以及单独的包安装目录,我们用虚拟环境可以很容易的在同一台计算机中安装和使用不同的 python 及其相关的包进行开发测试。
创建环境
我们使用 -m
选项来运行 venv
。
$ python3 -m venv /tmp/demoenv
根据不同的 python 解释器创建和打包,可能会带有不同版本的 pyvenv
命令行程序。下面这个与上面的等价(貌似并没有这个东西)。
$ pyvenv /tmp/demoenv
不过我们使用 -m venv
更好一些,因为这样我们可以手动选择python 解释器,这样我们就不会搞混版本号,也能知道生成的虚拟环境的导入目录是哪个。
虚拟环境中的小东西们
每个虚拟环境都有一个 bin
目录,里面放的是本地解释器和其他已安装的可执行脚本,include
目录里放的是创建 c 扩展相关的文件,lib
及其目录下的 site-packages
则是安装包时存放的位置。
$ ls -f /tmp/demoenv
bin/
include/
lib/
pyvenv.cfg
bin
目录中存放着几个「激活」脚本,可以在多个 unix shell 变种版本中使用。这些脚本可以将安装虚拟环境到 shell 的搜索路径中去,以确保 shell 知道程序安装到了这个环境中。当然这不是必须的,不过很方便。
$ ls -f /tmp/demoenv/bin
activate
activate.csh
activate.fish
easy_install*
easy_install-3.6*
pip*
pip3*
pip3.6*
python@
python3@
在支持符号链接的平台,将使用符号链接而不是复制可执行文件。在这种环境中,pip
会复制一份本地的,但解释器只是一个符号链接。
最后,环境中还有一个 pyvenv.cfg
文件,它的内容是环境的配置信息。home
变量指向 python 解释器的位置,venv
会根据这个创建环境。include-system-site-packages
是一个布尔值,表示在虚拟环境外的系统层的包是否可被虚拟环境使用。version
则表示创建的环境的python 版本。
pyvenv.cfg
home = /library/frameworks/python.framework/versions/3.6/bin
include-system-site-packages = false
version = 3.6.4
pip
和 setuptools
在虚拟环境中安装包非常好用,所以 pyvenv
默认把它们安装了。如果要安装一个干净的环境,需要在创建时指定 --without-pip
。
使用虚拟环境
虚拟环境通常用在运行不同版本的程序或测试某一个版本的程序的不同版本依赖。比如,在更新 sphinx 到另一个版本前,使用新旧两个版本分别测试一下是有必要的。让我们开始吧,先来创建两个虚拟环境。
$ python3 -m venv /tmp/sphinx1
$ python3 -m venv /tmp/sphinx2
之后我们安装它的不同版本进行测试。
$ /tmp/sphinx1/bin/pip install sphinx==1.3.6
collecting sphinx==1.3.6
using cached sphinx-1.3.6-py2.py3-none-any.whl
collecting pygments>=2.0 (from sphinx==1.3.6)
using cached pygments-2.2.0-py2.py3-none-any.whl
collecting sphinx-rtd-theme<2.0,>=0.1 (from sphinx==1.3.6)
using cached sphinx_rtd_theme-0.2.4-py2.py3-none-any.whl
collecting babel!=2.0,>=1.3 (from sphinx==1.3.6)
using cached babel-2.5.3-py2.py3-none-any.whl
collecting alabaster<0.8,>=0.7 (from sphinx==1.3.6)
using cached alabaster-0.7.10-py2.py3-none-any.whl
collecting jinja2>=2.3 (from sphinx==1.3.6)
using cached jinja2-2.10-py2.py3-none-any.whl
collecting docutils>=0.11 (from sphinx==1.3.6)
using cached docutils-0.14-py3-none-any.whl
collecting snowballstemmer>=1.1 (from sphinx==1.3.6)
using cached snowballstemmer-1.2.1-py2.py3-none-any.whl
collecting six>=1.4 (from sphinx==1.3.6)
using cached six-1.11.0-py2.py3-none-any.whl
collecting pytz>=0a (from babel!=2.0,>=1.3->sphinx==1.3.6)
using cached pytz-2018.3-py2.py3-none-any.whl
collecting markupsafe>=0.23 (from jinja2>=2.3->sphinx==1.3.6)
using cached markupsafe-1.0.tar.gz
installing collected packages: pygments, sphinx-rtd-theme, pytz,
babel, alabaster, markupsafe, jinja2, docutils, snowballstemmer,
six, sphinx
running setup.py install for markupsafe: started
running setup.py install for markupsafe: finished with
status 'done'
successfully installed jinja2-2.10 markupsafe-1.0 pygments-2.2.0
sphinx-1.3.6 alabaster-0.7.10 babel-2.5.3 docutils-0.14
pytz-2018.3 six-1.11.0 snowballstemmer-1.2.1 sphinx-rtd-
theme-0.2.4
$ /tmp/sphinx2/bin/pip install sphinx==1.4.4
collecting sphinx==1.4.4
using cached sphinx-1.4.4-py2.py3-none-any.whl
collecting imagesize (from sphinx==1.4.4)
using cached imagesize-1.0.0-py2.py3-none-any.whl
collecting pygments>=2.0 (from sphinx==1.4.4)
using cached pygments-2.2.0-py2.py3-none-any.whl
collecting snowballstemmer>=1.1 (from sphinx==1.4.4)
using cached snowballstemmer-1.2.1-py2.py3-none-any.whl
collecting alabaster<0.8,>=0.7 (from sphinx==1.4.4)
using cached alabaster-0.7.10-py2.py3-none-any.whl
collecting jinja2>=2.3 (from sphinx==1.4.4)
using cached jinja2-2.10-py2.py3-none-any.whl
collecting docutils>=0.11 (from sphinx==1.4.4)
using cached docutils-0.14-py3-none-any.whl
collecting babel!=2.0,>=1.3 (from sphinx==1.4.4)
using cached babel-2.5.3-py2.py3-none-any.whl
collecting six>=1.4 (from sphinx==1.4.4)
using cached six-1.11.0-py2.py3-none-any.whl
collecting markupsafe>=0.23 (from jinja2>=2.3->sphinx==1.4.4)
using cached markupsafe-1.0.tar.gz
collecting pytz>=0a (from babel!=2.0,>=1.3->sphinx==1.4.4)
using cached pytz-2018.3-py2.py3-none-any.whl
installing collected packages: imagesize, pygments,
snowballstemmer, alabaster, markupsafe, jinja2, docutils, pytz,
babel, six, sphinx
running setup.py install for markupsafe: started
running setup.py install for markupsafe: finished with
status 'done'
successfully installed jinja2-2.10 markupsafe-1.0 pygments-2.2.0
sphinx-1.4.4 alabaster-0.7.10 babel-2.5.3 docutils-0.14
imagesize-1.0.0 pytz-2018.3 six-1.11.0 snowballstemmer-1.2.1
这样我们就可以在两个环境中运行不同版本的 sphinx来测试同一份文件了。
$ /tmp/sphinx1/bin/sphinx-build --version
sphinx (sphinx-build) 1.3.6
$ /tmp/sphinx2/bin/sphinx-build --version
sphinx (sphinx-build) 1.4.4
参阅
- -- python 虚拟环境
- -- python 2 和 3 中都能使用的虚拟环境。
- -- virtualenv 的一系列 shell wrapper,让你管理大量环境更轻松。
- -- 转换 restructuretext(.rst)文件到 html,latex或其他格式的工具。
本译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 cc 协议,如果我们的工作有侵犯到您的权益,请及时联系380玩彩网官网入口。