简体   繁体   English

virtualenv 和 pyenv 是什么关系?

[英]What is the relationship between virtualenv and pyenv?

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.我最近学习了如何在我的工作流程中使用 virtualenv 和 virtualenvwrapper,但我在一些指南中看到了 pyenv,但我似乎无法理解 pyenv 是什么以及它与 virtualenv 有何不同/相似之处。 Is pyenv a better/newer replacement for virtualenv or a complimentary tool? pyenv 是 virtualenv 的更好/更新替代品还是免费工具? If the latter what does it do differently and how do the two (and virtualenvwrapper if applicable) work together?如果后者有什么不同,两者(和 virtualenvwrapper,如果适用)如何协同工作?

Pyenv and virtualenv are very different tools that work in different ways to do different things: Pyenvvirtualenv是非常不同的工具,它们以不同的方式工作来做不同的事情:

  • Pyenv is 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. Pyenv是一个 bash 扩展——不能在 Windows 上运行——它会拦截你对 python、pip 等的调用,将它们定向到几个系统 python 工具链之一。 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.因此,您始终可以使用已安装在所选 Python 版本中的所有库——因此,这对于必须在不同版本的 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. VirtualEnv是纯 python,因此可以在任何地方使用,它会复制(可选)特定版本的python 和 pip 本地到激活环境,其中可能包含或不包含指向当前系统工具链的链接,如果没有,您可以只将一个已知的库子集安装到该环境中。 As such it is almost certainly much better for testing and deployment as you know exactly which libraries, at which versions, are used and a global change will not impact your module.因此,它几乎肯定会更好地用于测试和部署,因为您确切地知道使用哪些库、哪些版本,并且全局更改不会影响您的模块。

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.请注意,从 Python 3.3 开始,有一个名为 venv 的 VirtualEnv 内置实现(在某些安装中,有一个名为 pyvenv 的包装器 - 此包装器在 Python 3.6 中已弃用),可能应该优先使用它。 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/path or you can use the excellent py python selector on windows with py -3 -m venv desired/env/path .为了避免包装器可能出现的问题,通过使用/path/to/python3 -m venv desired/env/path直接使用它通常是一个好主意,或者您可以使用py -3 -m venv desired/env/path在 windows 上使用出色的py python 选择器py -3 -m venv desired/env/path It will create the directory specified with desired/env/path configure and populate it appropriately.它将创建使用desired/env/path配置指定的目录并适当地填充它。 In general it is very much like using VirtualEnv.一般来说,它非常像使用 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:有许多工具值得一提和考虑,因为它们可以帮助使用上述一个或多个:

  • VirtualEnvWrapper Manage and simplify the use and management of VirtualEnv - Cross Platform . VirtualEnvWrapper管理和简化 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 . pyenv-virtualenvpyenv-installer 安装,它提供了用于管理和连接 VirtualEnv 的 PyEnv 工具——这样你就可以拥有一个包含多个 python 版本的基础安装,并在每个版本中创建隔离的环境——Linux/OS-十 Suggested by Johann Visagie由 Johann Visagie 推荐
  • PyInstaller can 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 of python 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. PyInstaller可以获取您的 Python 代码,可能是在 VirtualEnv 下开发和测试的,并将其捆绑在一起,以便它可以运行一个没有安装您的Python版本的平台 - 请注意,它不是交叉编译器,您将需要一个 Windows(虚拟-) 机器来构建 Windows 安装等,但即使在您可以确定将安装 python 但不能确定 python 的版本和所有库是否与您的代码兼容的情况下,它也很方便。

Short version:精简版:

  • virtualenv allows you to create local (per-directory), independent python installations by cloning from existing ones virtualenv允许您通过从现有的克隆来创建本地(每个目录)独立的 python 安装
  • pyenv allows you to install (build from source) different versions of Python alongside each other; pyenv允许您一起安装(从源代码构建)不同版本的 Python; you can then clone them with virtualenv or use pyenv to select which one to run at any given time然后您可以使用 virtualenv 克隆它们或使用 pyenv 选择在任何给定时间运行哪个

Longer version:更长的版本:

Virtualenv allows you to create a custom Python installation eg in a subdirectory of your project. Virtualenv 允许您创建自定义 Python 安装,例如在您的项目的子目录中。 This is done by cloning from an existing Python installation somewhere on your system (some files are copied, some are reused/shared to save space).这是通过从系统上某处的现有 Python 安装克隆来完成的(一些文件被复制,一些被重用/共享以节省空间)。 Each of your projects can thus have their own python (or even several) under their respective virtualenv.因此,您的每个项目都可以在各自的 virtualenv 下拥有自己的python (甚至几个)。 It is perfectly fine for some/all virtualenvs to even have the same version of python (eg 3.8.5) without conflict - they live separately and don't know about each other.对于某些/所有 virtualenv 来说,即使拥有相同版本的python (例如 3.8.5)也没有冲突,这完全没问题——它们分开生活,彼此不了解。 If you want to use any of those python s from shell, you have to activate it (by running a script which will temporarily modify your PATH to ensure that that virtualenv's bin/ directory comes first).如果您想从 shell 使用这些python的任何一个,您必须activate它(通过运行一个脚本来临时修改您的PATH以确保该 virtualenv 的bin/目录首先出现)。 From that point, calling python (or pip etc.) will invoke that virtualenv's version until you deactivate it (which restores the PATH ).从那时起,调用python (或pip等)将调用该 virtualenv 的版本,直到您deactivate它(恢复PATH )。 It is also possible to call into a virtualenv Python using its absolute path - this can be useful eg when invoking Python from a script.也可以使用绝对路径调用 virtualenv Python - 这在例如从脚本调用 Python 时很有用。

Pyenv operates on a wider scale than virtualenv. Pyenv 的运行范围比 virtualenv 更广泛。 It is used to install (build from source) arbitrary versions of Python (it holds a register of available versions).它用于安装(从源代码构建)任意版本的 Python(它拥有可用版本的寄存器)。 By default, they're all installed alongside each other under ~/.pyenv , so they're "more global" than virtualenv.默认情况下,它们都安装在~/.pyenv ,因此它们比 virtualenv 更“全局”。 Then, it allows you to configure which version of Python to run when you use the python command (without virtualenv).然后,它允许您在使用python命令(不带 virtualenv)时配置要运行的 Python 版本。 This can be done at a global level or, separately, per directory (by placing a .python-version file in a directory).这可以在全局级别完成,也可以单独完成,每个目录(通过将.python-version文件放在目录中)。 It's done by prepending pyenv's shim python script to your PATH (permanently, unlike in virtualenv) which then decides which "real" python to invoke.它是通过将 pyenv 的 shim python脚本添加到您的PATH (永久地,与 virtualenv 不同)来完成的,然后决定调用哪个“真正的” python You can even configure pyenv to call into one of your virtualenv pythons (by using the pyenv-virtualenv plugin).您甚至可以配置 pyenv 以调用您的 virtualenv python 之一(通过使用pyenv-virtualenv插件)。 You can also duplicate Python versions (by giving them different names) and let them diverge.您还可以复制 Python 版本(通过给它们不同的名称)并让它们分开。

Using pyenv can be a convenient way of installing Python for subsequent virtualenv use.使用 pyenv 可以方便地安装 Python 以供后续 virtualenv 使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM