Python virtualenv 和 pyenv 是什么关系?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29950300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 05:12:05  来源:igfitidea点击:

What is the relationship between virtualenv and pyenv?

pythonvirtualenvvirtualenvwrapperpyenv

提问by truth1ness

I recently learned how to use virtualenv and virtualenvwrapper in my workflow but I've seen pyenv mentioned in a few guides but I can't seem to get an understanding of what pyenv is and how it is different/similar to virtualenv. Is pyenv a better/newer replacement for virtualenv or a complimentary tool? If the latter what does it do differently and how do the two (and virtualenvwrapper if applicable) work together?

我最近学习了如何在我的工作流程中使用 virtualenv 和 virtualenvwrapper,但我在一些指南中看到了 pyenv,但我似乎无法理解 pyenv 是什么以及它与 virtualenv 有何不同/相似之处。pyenv 是 virtualenv 的更好/更新替代品还是免费工具?如果后者有什么不同,两者(和 virtualenvwrapper,如果适用)如何协同工作?

采纳答案by Steve Barnes

Pyenvand virtualenvare very different tools that work in different ways to do different things:

Pyenvvirtualenv是非常不同的工具,它们以不同的方式工作来做不同的事情:

  • Pyenvis a bash extension - will not work on Windows - that intercepts your calls to python, pip, etc., to direct them to one of several of the system python tool-chains. So you always have all the libraries that you have installed in the selected python version available - as such it is good for users who have to switch between different versions of python.

  • VirtualEnv, is pure python so works everywhere, it makes a copy of, optionally a specific version of,python and pip local to the activate environment which may or may not include links to the current system tool-chain, if it does not you can install just a known subset of libraries into that environment. As such it is almost certainly much better for testing and deployment as you know exactlywhich libraries, at which versions, are used and a global change will not impact your module.

  • Pyenv是一个 bash 扩展——不能在 Windows 上运行——它会拦截你对 python、pip 等的调用,将它们定向到几个系统 python 工具链之一。因此,您始终可以使用已安装在所选 Python 版本中的所有库——因此,这对于必须在不同版本的 Python 之间切换的用户来说非常有用。

  • VirtualEnv是纯 python,因此可以在任何地方使用,它可以复制(可选)特定版本的python 和 pip 本地到激活环境,其中可能包含也可能不包含指向当前系统工具链的链接,如果没有,您可以只将一个已知的库子集安装到该环境中。因此,它几乎肯定会更好地用于测试和部署,因为您确切地知道使用哪些库、哪些版本,并且全局更改不会影响您的模块。

venv python > 3.3

venv python > 3.3

Note that from Python 3.3 onward there is a built in implementation of VirtualEnv called venv (with, on some installations a wrapper called pyvenv - this wrapper is deprecated in Python 3.6), which should probably be used in preference. To avoid possible issues with the wrapper it is often a good idea to use it directly by using /path/to/python3 -m venv desired/env/pathor you can use the excellent pypython selector on windows with py -3 -m venv desired/env/path. It will create the directory specified with desired/env/pathconfigure and populate it appropriately. In general it is very much like using VirtualEnv.

请注意,从 Python 3.3 开始,有一个名为 venv 的 VirtualEnv 内置实现(在某些安装中,有一个名为 pyvenv 的包装器 - 此包装器在 Python 3.6 中已弃用),可能应该优先使用它。为了避免包装器可能出现的问题,通过使用直接使用它通常是一个好主意,/path/to/python3 -m venv desired/env/path或者您可以py在带有py -3 -m venv desired/env/path. 它将创建用desired/env/pathconfigure指定的目录并适当地填充它。一般来说,它非常像使用 VirtualEnv。

Additional Tools

附加工具

There are a number of tools that it is worth mentioning, and considering, as they can help with the use of one or more of the above:

有许多工具值得一提和考虑,因为它们可以帮助使用上述一个或多个:

  • VirtualEnvWrapperManage and simplify the use and management of VirtualEnv - Cross Platform.
  • pyenv-virtualenv, installed by pyenv-installer, which gives PyEnv tools for managing and interfacing to VirtualEnv - with this you can have a base installation that includes more than one version of python and create isolated environments within each of them - Linux/OS-X. Suggested by Johann Visagie
  • PyInstallercan take your python code, possibly developed & tested under VirtualEnv, and bundle it up so that it can run one platforms that do not have your version ofpython installed - Note that it is not a cross compiler you will need a Windows (virtual-)machine to build Windows installs, etc., but it can be handy even where you can be sure that python will be installed but cannot be sure that the version of python and all the libraries will be compatible with your code.
  • VirtualEnvWrapper管理和简化 VirtualEnv- Cross Platform的使用和管理。
  • pyenv-virtualenvpyenv-installer 安装,它提供了用于管理和连接 VirtualEnv 的 PyEnv 工具 - 有了这个,你可以拥有一个包含多个 python 版本的基础安装,并在每个版本中创建隔离的环境 - Linux/OS-十由 Johann Visagie 推荐
  • PyInstaller可以获取您的 Python 代码(可能在 VirtualEnv 下开发和测试),并将其捆绑在一起,以便它可以运行一个没有安装您的Python版本的平台- 请注意,它不是交叉编译器,您将需要一个 Windows(虚拟-) 机器来构建 Windows 安装等,但即使在您可以确定将安装 python 但无法确定 python 的版本和所有库是否与您的代码兼容的情况下,它也很方便。

回答by B?a?ej Czapp

virtualenvallows you to create a custom Python installation e.g. in a subdirectory of your project. Each of your projects can thus have their own python(or even several) under their respective virtualenv. It is perfectly fine for some/all virtualenvs to even have the same version of python(e.g. 2.7.16) without conflict - they live separately and don't know of each other. If you want to use any of those pythons, you have to activateit (by running a script which will temporarily modify your PATHto ensure that that virtualenv's bin/directory comes first). From that point, calling python(or pipetc.) will invoke that virtualenv's version until you deactivateit (which restores the PATH).

virtualenv允许您创建自定义 Python 安装,例如在项目的子目录中。因此,您的每个项目都可以python在各自的 virtualenv 下拥有自己的(甚至几个)。对于某些/所有 virtualenvs 甚至具有相同版本python(例如 2.7.16)而没有冲突是完全没问题的——它们分开生活并且彼此不认识。如果你想使用这些pythons 中的任何一个,你必须使用activate它(通过运行一个脚本来临时修改你PATH以确保 virtualenv 的bin/目录排在第一位)。从那时起,调用python(或pip等)将调用该 virtualenv 的版本,直到您调用deactivate它(恢复PATH)。

pyenvoperates on a wider scale than virtualenv- it holds a register of Python installations (and can be used to install new ones) and allows you to configure which version of Python to run when you use the pythoncommand. Sounds similar but practical use is a bit different. It works by prepending its shim pythonscript to your PATH(permanently) and then deciding which "real" pythonto invoke. You can even configure pyenv to call into one of your virtualenv pythons (by using the pyenv-virtualenvplugin). Python versions you install using pyenvgo into its $(pyenv root)/versions/directory (by default, pyenv root is ~/.pyenv) so are more 'global' than virtualenv. Ordinarily, you can't duplicate Python versions installed through pyenv, at least doing so is not the main idea.

pyenv运行范围比virtualenv- 它拥有 Python 安装的寄存器(并可用于安装新的),并允许您配置在使用python命令时运行的 Python 版本。听起来很相似,但实际使用有点不同。它的工作原理是将其 shimpython脚本预先添加到您的PATH(永久)中,然后决定python调用哪个“真实” 。您甚至可以配置 pyenv 以调用您的 virtualenv python 之一(通过使用pyenv-virtualenv插件)。您使用安装的 Python 版本pyenv进入其$(pyenv root)/versions/目录(默认情况下,pyenv 根目录为 ~/.pyenv),因此比 virtualenv 更“全局”。通常,您不能复制通过 安装的 Python 版本pyenv,至少这样做不是主要思想。

To create a virtualenv with a specific Python version, you need to have that version somewhere in your system (whether it's on the PATHor not) and essentially clone it into your newly created virtualenv. Of course, one way to obtain a particular version is to install it via pyenv. Once that's done, individual virtualenvs are free to diverge by having different modules (or versions thereof) installed into them.

要创建具有特定 Python 版本的 virtualenv,您需要在系统中的某个位置(无论是否在其中PATH)具有该版本,并且基本上将其克隆到新创建的 virtualenv 中。当然,获取特定版本的一种方法是通过pyenv. 一旦完成,各个 virtualenv 就可以通过安装不同的模块(或其版本)来自由地发散。

In short:

简而言之:

  • virtualenvallows you to create local, independent python installations by cloning from existing ones
  • pyenvallows you to install different versions of python simultaneously (either system-wide or just for the local user) and then choose which of the multitude of pythons to run at any given time (including those created by virtualenv or Anaconda)
  • virtualenv允许您通过从现有的克隆来创建本地的、独立的 python 安装
  • pyenv允许您同时安装不同版本的 python(系统范围内或仅用于本地用户),然后选择在任何给定时间运行的众多 python 中的哪一个(包括由 virtualenv 或 Anaconda 创建的那些)