本文翻译自:What is the difference between venv, pyvenv, pyenv, virtualenv, virtualenvwrapper, pipenv, etc?
Python 3.3 includes in its standard library the new package venv
. Python 3.3在其标准库中包括新的软件包venv
。 What does it do, and how does it differ from all the other packages that seem to match the regex (py)?(v|virtual|pip)?env
? 它有什么作用?与似乎与正则表达式(py)?(v|virtual|pip)?env
匹配的所有其他软件包有什么区别(py)?(v|virtual|pip)?env
?
参考:https://stackoom.com/question/2oRBj/venv-pyvenv-pyenv-virtualenv-virtualenvwrapper-pipenv等有什么区别
virtualenv
is a very popular tool that creates isolated Python environments for Python libraries. virtualenv
是一种非常流行的工具,可为Python库创建隔离的Python环境。 If you're not familiar with this tool, I highly recommend learning it, as it is a very useful tool, and I'll be making comparisons to it for the rest of this answer. 如果您不熟悉此工具,我强烈建议您学习它,因为它是非常有用的工具,在本答案的其余部分中,我将对其进行比较。
It works by installing a bunch of files in a directory (eg: env/
), and then modifying the PATH
environment variable to prefix it with a custom bin
directory (eg: env/bin/
). 它的工作方式是在目录中安装一堆文件(例如: env/
),然后修改PATH
环境变量以在其前面添加自定义bin
目录(例如: env/bin/
)。 An exact copy of the python
or python3
binary is placed in this directory, but Python is programmed to look for libraries relative to its path first, in the environment directory. python
或python3
二进制文件的精确副本位于此目录中,但Python编程为在环境目录中首先查找与其路径相关的库。 It's not part of Python's standard library, but is officially blessed by the PyPA (Python Packaging Authority). 它不是Python标准库的一部分,但受到PyPA(Python包装管理局)的正式认可。 Once activated, you can install packages in the virtual environment using pip
. 激活后,您可以使用pip
在虚拟环境中安装软件包。
pyenv
is used to isolate Python versions. pyenv
用于隔离Python版本。 For example, you may want to test your code against Python 2.6, 2.7, 3.3, 3.4 and 3.5, so you'll need a way to switch between them. 例如,您可能想针对Python 2.6、2.7、3.3、3.4和3.5测试代码,因此需要一种在它们之间切换的方法。 Once activated, it prefixes the PATH
environment variable with ~/.pyenv/shims
, where there are special files matching the Python commands ( python
, pip
). 激活后,它会在PATH
环境变量前加上~/.pyenv/shims
前缀,其中有匹配Python命令( python
, pip
)的特殊文件。 These are not copies of the Python-shipped commands; 这些不是Python附带命令的副本。 they are special scripts that decide on the fly which version of Python to run based on the PYENV_VERSION
environment variable, or the .python-version
file, or the ~/.pyenv/version
file. 它们是特殊的脚本,它们根据PYENV_VERSION
环境变量, .python-version
文件或~/.pyenv/version
文件,即时确定要运行哪个版本的Python。 pyenv
also makes the process of downloading and installing multiple Python versions easier, using the command pyenv install
. pyenv
还可以使用pyenv install
命令pyenv install
下载和安装多个Python版本的过程。
pyenv-virtualenv
is a plugin for pyenv
by the same author as pyenv
, to allow you to use pyenv
and virtualenv
at the same time conveniently. pyenv-virtualenv
是pyenv
的同一作者的pyenv
,可让您方便地同时使用pyenv
和virtualenv
。 However, if you're using Python 3.3 or later, pyenv-virtualenv
will try to run python -m venv
if it is available, instead of virtualenv
. 但是,如果您使用的是Python 3.3或更高版本,则pyenv-virtualenv
将尝试运行python -m venv
如果可用),而不是virtualenv
。 You can use virtualenv
and pyenv
together without pyenv-virtualenv
, if you don't want the convenience features. 如果您不希望使用便利功能,则可以将virtualenv
和pyenv
一起使用,而无需使用pyenv-virtualenv
。
virtualenvwrapper
is a set of extensions to virtualenv
(see docs ). virtualenvwrapper
是一组扩展的virtualenv
(参见文档 )。 It gives you commands like mkvirtualenv
, lssitepackages
, and especially workon
for switching between different virtualenv
directories. 它为您提供mkvirtualenv
, lssitepackages
等命令,尤其是在不同的virtualenv
目录之间切换的workon
。 This tool is especially useful if you want multiple virtualenv
directories. 如果要多个virtualenv
目录,此工具特别有用。
pyenv-virtualenvwrapper
is a plugin for pyenv
by the same author as pyenv
, to conveniently integrate virtualenvwrapper
into pyenv
. pyenv-virtualenvwrapper
是pyenv
的同一作者的pyenv
,可方便地将virtualenvwrapper
集成到pyenv
。
pipenv
, by Kenneth Reitz (the author of requests
), is the newest project in this list. Kenneth Reitz( requests
的作者)的pipenv
是该列表中的最新项目。 It aims to combine Pipfile
, pip
and virtualenv
into one command on the command-line. 它旨在将Pipfile
, pip
和virtualenv
为命令行上的一个命令。 The virtualenv
directory typically gets placed in ~/.local/share/virtualenvs/XXX
, with XXX
being a hash of the path of the project directory. 通常将virtualenv
目录放置在~/.local/share/virtualenvs/XXX
,其中XXX
是项目目录路径的哈希。 This is different from virtualenv
, where the directory is typically in the current working directory. 这与virtualenv
不同,后者的目录通常位于当前工作目录中。
The Python Packaging Guide recommends pipenv
when developing Python applications (as opposed to libraries). 《 Python打包指南》 建议在开发Python应用程序(而不是库)时使用pipenv
。 There does not seem to be any plans to support venv
instead of virtualenv
( #15 ). 似乎没有任何计划支持venv
而不是virtualenv
( #15 )。 Confusingly, its command-line option --venv
refers to the virtualenv
directory, not venv
, and similarly, the environment variable PIPENV_VENV_IN_PROJECT
affects the location of the virtualenv
directory, not venv
directory ( #1919 ). 令人困惑的是,其命令行选项--venv
是指virtualenv
目录,而不是venv
,同样,环境变量PIPENV_VENV_IN_PROJECT
会影响virtualenv
目录的位置,而不是venv
目录( #1919 )。
pyvenv
is a script shipped with Python 3 but deprecated in Python 3.6 as it had problems (not to mention the confusing name). pyvenv
是Python 3附带的脚本,但由于存在问题(更不用说混乱的名称了)而在Python 3.6中不推荐使用 。 In Python 3.6+, the exact equivalent is python3 -m venv
. 在Python 3.6+中,确切的等效项是python3 -m venv
。
venv
is a package shipped with Python 3, which you can run using python3 -m venv
(although for some reason some distros separate it out into a separate distro package, such as python3-venv
on Ubuntu/Debian). venv
是Python 3随附的软件包,您可以使用python3 -m venv
运行该python3 -m venv
(尽管出于某些原因,某些发行版将其分成了单独的发行版软件包,例如Ubuntu / Debian上的python3-venv
)。 It serves a similar purpose to virtualenv
, and works in a very similar way, but it doesn't need to copy Python binaries around (except on Windows). 它的作用与virtualenv
相似,并且工作方式非常相似,但不需要复制Python二进制文件(Windows除外)。 Use this if you don't need to support Python 2. At the time of writing, the Python community seems to be happy with virtualenv
and I haven't heard much talk of venv
. 如果您不需要支持Python 2,请使用它。在撰写本文时,Python社区似乎对virtualenv
感到满意,而且我还没有听到太多有关venv
。
Most of these tools complement each other. 这些工具大多相互补充。 For instance, pipenv
integrates pip
, virtualenv
and even pyenv
if desired. 例如,如果需要, pipenv
集成了pip
, virtualenv
甚至pyenv
。 The only tools that are true alternatives to each other here are venv
and virtualenv
. venv
和virtualenv
是venv
可以彼此替代的工具。
This is my personal recommendation for beginners: start by learning virtualenv
and pip
, tools which work with both Python 2 and 3 and in a variety of situations, and pick up the other tools once you start needing them. 这是我对初学者的个人建议:首先学习virtualenv
和pip
,这些工具在各种情况下virtualenv
与Python 2和3一起使用,并在开始需要它们时选择其他工具。
I would just avoid the use of virtualenv
after Python3.3+ and instead use the standard shipped library venv
. 我只是避免在Python3.3 +之后使用virtualenv
,而是使用标准附带的库venv
。 To create a new virtual environment you would type: 要创建新的虚拟环境,请输入:
$ python3 -m venv
virtualenv
tries to copy the Python binary into the virtual environment's bin directory. virtualenv
尝试将Python二进制文件复制到虚拟环境的bin目录中。 However it does not update library file links embedded into that binary, so if you build Python from source into a non-system directory with relative path names, the Python binary breaks. 但是,它不会更新嵌入到该二进制文件中的库文件链接,因此,如果您将Python从源代码构建到具有相对路径名的非系统目录中,则Python二进制文件会中断。 Since this is how you make a copy distributable Python, it is a big flaw. 由于这是使副本可分发的Python的方式,因此这是一个很大的缺陷。 BTW to inspect embedded library file links on OS X, use otool
. BTW要检查OS X上的嵌入式库文件链接,请使用otool
。 For example from within your virtual environment, type: 例如,在您的虚拟环境中,键入:
$ otool -L bin/python
python:
@executable_path/../Python (compatibility version 3.4.0, current version 3.4.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1238.0.0)
Consequently I would avoid virtualenvwrapper
and pipenv
. 因此,我将避免使用virtualenvwrapper
和pipenv
。 pyvenv
is deprecated. pyvenv
已弃用。 pyenv
seems to be used often where virtualenv
is used but I would stay away from it also since I think venv
also does what pyenv
is built for. pyenv
似乎经常在使用virtualenv
的地方使用,但由于我认为venv
也会做pyenv
,因此我也不pyenv
。
venv
creates virtual environments in the shell that are fresh and sandboxed , with user-installable libraries , and it's multi-python safe . venv
使用用户可安装的库在外壳中创建全新的 , 沙盒化的虚拟环境,并且它是多Python安全的 。 Fresh because virtual environments only start with the standard libraries that ship with python, you have to install any other libraries all over again with pip install
while the virtual environment is active. 新鲜 ,因为虚拟环境只能用标准库启动船舶与蟒蛇,你必须与各地重新安装任何其他库pip install
在虚拟环境是积极的。 Sandboxed because none of these new library installs are visible outside the virtual environment, so you can delete the whole environment and start again without worrying about impacting your base python install. 沙盒化,因为在虚拟环境外部看不到这些新库安装,因此您可以删除整个环境并重新启动,而不必担心会影响基本的python安装。 User-installable libraries because the virtual environment's target folder is created without sudo
in some directory you already own, so you won't need sudo
permissions to install libraries into it. 用户可安装的库,因为在您已经拥有的某个目录中创建了虚拟环境的目标文件夹而没有sudo
,因此您不需要sudo
权限即可将库安装到其中。 Finally it is multi-python safe , since when virtual environments activate, the shell only sees the python version (3.4, 3.5 etc.) that was used to build that virtual environment. 最终,它是多python安全的 ,因为在激活虚拟环境时,shell仅看到用于构建该虚拟环境的python版本(3.4、3.5等)。
pyenv
is similar to venv
in that it lets you manage multiple python environments. pyenv
类似于venv
,它可以让你管理多个蟒蛇环境。 However with pyenv
you can't conveniently rollback library installs to some start state and you will likely need admin
privileges at some point to update libraries. 但是,使用pyenv
您无法方便地将库安装回滚到某些开始状态,并且在某些时候您可能需要admin
权限才能更新库。 So I think it is also best to use venv
. 所以我认为最好还是使用venv
。
In the last couple of years I have found many problems in build systems (emacs packages, python standalone application builders, installers...) that ultimately come down to issues with virtualenv
. 在过去的两年中,我发现了构建系统中的许多问题(emacs软件包,python独立应用程序构建器,安装程序...),最终归结为virtualenv
问题。 I think python will be a better platform when we eliminate this additional option and only use venv
. 我认为当我们取消此附加选项并仅使用venv
时,python将是一个更好的平台。