标签归档:pyenv

virtualenv和pyenv之间是什么关系?

问题:virtualenv和pyenv之间是什么关系?

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

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?


回答 0

Pyenvvirtualenv是非常不同的工具,它们以不同的方式工作以执行不同的操作:

  • Pyenv是bash扩展- 不适用于Windows-会拦截您对python,pip等的调用,以将其定向到多个系统python工具链之一。因此,您始终具有在选定的python版本中安装的所有库,因此,这对于必须在不同版本的python之间进行切换的用户而言非常有用。

  • VirtualEnv是纯python,因此可在任何地方使用,它会在激活环境中本地复制python和pip 的副本,或者可选地复制特定版本,该环境可能包含也可能不包含指向当前系统工具链的链接,如果不能,则可以仅将已知的库子集安装到该环境中。这样一来,几乎可以肯定,对于测试和部署而言,要好得多,因为您确切知道使用哪个库,使用了哪个版本,并且全局更改不会影响您的模块。

venv python> 3.3

请注意,从Python 3.3开始,有一个名为venv的VirtualEnv内置实现(在某些安装中,有一个名为pyvenv的包装器- 在Python 3.6中已弃用该包装器),应该优先使用它。为避免包装程序可能出现问题,通常最好直接使用/path/to/python3 -m venv desired/env/path或使用pyWindows上的优秀python选择器来使用它py -3 -m venv desired/env/path。它将创建用desired/env/pathconfigure 指定的目录并适当地填充它。通常,这非常类似于使用VirtualEnv。

其他工具

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

  • VirtualEnvWrapper管理和简化VirtualEnv- Cross平台的使用和管理。
  • pyenv-virtualenvpyenv-installer安装,为PyEnv工具提供了用于管理和与VirtualEnv交互的工具-通过此工具,您可以进行基本安装,包括多个版本的python,并在每个版本中创建隔离的环境-Linux / OS- XJohann Visagie建议
  • PyInstaller可以获取可能在VirtualEnv下开发和测试的python代码,并将其捆绑在一起,以便它可以运行未安装python 版本的平台-请注意,它不是交叉编译器,因此您需要Windows(虚拟) -)机器来构建Windows安装等,但是即使您可以确定将安装python但不能确定python的版本和所有库是否与您的代码兼容,它也可以派上用场。

Pyenv and virtualenv are very different tools that work in different ways to do different things:

  • 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. 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 exactly which libraries, at which versions, are used and a global change will not impact your module.

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/path or you can use the excellent py python selector on windows with py -3 -m venv desired/env/path. It will create the directory specified with desired/env/path configure and populate it appropriately. In general it is very much like using 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.
  • 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
  • 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.

回答 1

virtualenv允许您在项目的子目录中创建自定义Python安装。因此,您的每个项目python在其各自的virtualenv下都可以拥有自己的(甚至几个)项目。某些/所有virtualenv甚至具有相同版本python(例如2.7.16)而没有冲突是完全可以的-它们独立存在并且彼此不认识。如果要使用其中任何一个python,则必须使用activate它(通过运行一个脚本来临时修改您的脚本,PATH以确保virtualenv的bin/目录位于第一位)。从那时起,调用python(或其他方法pip)将调用该virtualenv的版本,直到您使用deactivate它(还原PATH)为止。

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

要创建具有特定Python版本的virtualenv,您需要将该版本放置在系统中的某个位置(无论是否在该版本中PATH),并将其本质上克隆到新创建的virtualenv中。当然,获得特定版本的一种方法是通过安装它pyenv。完成此操作后,可以通过将不同的模块(或其版本)安装到各个虚拟环境中来自由进行区分。

简而言之:

  • virtualenv 允许您通过从现有安装中进行克隆来创建本地独立的python安装
  • pyenv 允许您同时安装不同版本的python(在系统范围内或仅针对本地用户),然后选择在任意给定时间运行哪些python(包括由virtualenv或Anaconda创建的)

virtualenv allows 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 activate it (by running a script which will temporarily modify your PATH to ensure that that virtualenv’s bin/ directory comes first). From that point, calling python (or pip etc.) will invoke that virtualenv’s version until you deactivate it (which restores the PATH).

pyenv operates 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 python command. Sounds similar but practical use is a bit different. It works by prepending its shim python script to your PATH (permanently) and then deciding which “real” python to invoke. You can even configure pyenv to call into one of your virtualenv pythons (by using the pyenv-virtualenv plugin). Python versions you install using pyenv go 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.

To create a virtualenv with a specific Python version, you need to have that version somewhere in your system (whether it’s on the PATH or 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.

In short:

  • virtualenv allows you to create local, independent python installations by cloning from existing ones
  • pyenv allows 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)

pyenv,virtualenv,anaconda有什么区别?

问题:pyenv,virtualenv,anaconda有什么区别?

我是一个尝试学习python的红宝石程序员。我很喜欢pyenv,因为它就像来自rbenv的复制和粘贴一样。Pyenv帮助在系统中拥有多个版本的python,并且无需隔离系统的敏感部分即可隔离python。

我想每个python安装都带有pip包。我仍然不明白的是,有很多好的python库建议使用此virtualenv和anaconda。我什至可以找到pyenv的virtualenv插件。

现在,我对这两个pyenv和virtualenv的目的感到困惑。更糟糕的是在pyenv内部有一个virtualenv插件。

我的问题是:

  • pyenv和virtualenv有什么区别?
  • 在pyenv和virtualenv中使用pip命令有什么区别吗?
  • pyenv virutalenv做什么?

您的示例解释将不胜感激。

I am a ruby programmer trying to learn python. I am pretty family with pyenv since it is like a copy and paste from rbenv. Pyenv helps allow to have more than one version of python in a system and also to isolate the python without touching sensitive part of system.

I suppose every python installation come with pip package. What I still don’t understand is, there are many good python libs out there that suggest to use this virtualenv and anaconda. I can even find virtualenv plugin for pyenv.

Now I am getting confused with the purpose of these two pyenv and virtualenv. worse inside pyenv there is a virtualenv plugin.

my questions are:

  • what is the difference between pyenv and virtualenv?
  • Is there any difference in using pip command inside both pyenv and virtualenv?
  • what does this pyenv virutalenv do?

your explanation with example will be highly appreciated.


回答 0

编辑:pip这里也值得一提,conda并且pip具有与该主题相关的异同

pip:Python软件包管理器。

  • 您可能将其pip视为ruby gem命令的python等效项
  • pip 默认情况下不包含在python中。
  • 您可以使用homebrew安装Python ,它将自动安装pip:brew install python
  • OSX的最终版本默认不包含pip。要将pip添加到Mac系统的python版本中,您可以sudo easy_install pip
  • 您可以使用PyPI查找和发布python软件包:Python软件包索引
  • requirements.txt文件与ruby相当 gemfile
  • 要创建需求文本文件, pip freeze > requirements.txt
  • 请注意,此时,我们已经在系统上安装了python,并创建了requirements.txt文件,该文件概述了系统上已安装的所有python软件包。

pyenv:Python版本管理器

  • 从文档 pyenv可以轻松在多个版本的Python之间切换。它简单,简单,遵循UNIX的一站式工具的传统,可以很好地完成一件事。该项目是从rbenv和ruby-build分叉的,并针对Python进行了修改。
  • 很多人 不愿使用python3
  • 如果您需要使用其他版本的python,则pyenv可以轻松进行管理。

virtualenv:Python环境管理器。

  • 从文档要解决的基本问题是依赖项和版本之一以及间接权限。假设您有一个需要使用LibFoo版本1的应用程序,但是另一个应用程序需要版本2。如果将所有内容都安装到/usr/lib/python2.7/site-packages(或平台的标准位置是什么)中,那么很容易在无意中升级不应升级的应用程序的情况下结束。
  • 要创建一个virtualenv,只需调用virtualenv ENV,其中ENV是用于放置新虚拟环境的目录。
  • 要初始化virtualenv,您需要source ENV/bin/activate。要停止使用,只需调用deactivate
  • 激活后virtualenv,您可以通过运行pip install -r项目requirements.txt文件来安装工作区的所有软件包要求。

Anaconda:程序包经理+环境经理+其他科学图书馆。

  • 来自文档Anaconda 4.2.0包括易于安装的Python(2.7.12、3.4.5和/或3.5.2),以及100多种经过预先构建和测试的科学和分析Python软件包的更新,其中包括NumPy,Pandas ,SciPy,Matplotlib和IPython,通过一个简单的工具即可提供620多个软件包conda install <packagename>
  • 作为Web开发人员,我没有使用Anaconda。包括所有程序包,容量约为3GB。
  • 有一个精简miniconda版,似乎比使用pip+ 更简单virtualenv,尽管我个人没有使用它的经验。
  • 尽管conda允许您安装软件包,但是这些软件包与PyPI软件包是分开的,因此,根据您需要安装的软件包的类型,您可能仍然需要额外使用pip。

也可以看看:

Edit: It’s worth mentioning pip here as well, as conda and pip have similarities and differences that are relevant to this topic.

pip: the Python Package Manager.

  • You might think of pip as the python equivalent of the ruby gem command
  • pip is not included with python by default.
  • You may install Python using homebrew, which will install pip automatically: brew install python
  • The final version of OSX did not include pip by default. To add pip to your mac system’s version of python, you can sudo easy_install pip
  • You can find and publish python packages using PyPI: The Python Package Index
  • The requirements.txt file is comparable to the ruby gemfile
  • To create a requirements text file, pip freeze > requirements.txt
  • Note, at this point, we have python installed on our system, and we have created a requirements.txt file that outlines all of the python packages that have been installed on your system.

pyenv: Python Version Manager

  • From the docs: pyenv lets you easily switch between multiple versions of Python. It’s simple, unobtrusive, and follows the UNIX tradition of single-purpose tools that do one thing well. This project was forked from rbenv and ruby-build, and modified for Python.
  • Many folks hesitate to use python3.
  • If you need to use different versions of python, pyenv lets you manage this easily.

virtualenv: Python Environment Manager.

  • From the docs: The basic problem being addressed is one of dependencies and versions, and indirectly permissions. Imagine you have an application that needs version 1 of LibFoo, but another application requires version 2. How can you use both these applications? If you install everything into /usr/lib/python2.7/site-packages (or whatever your platform’s standard location is), it’s easy to end up in a situation where you unintentionally upgrade an application that shouldn’t be upgraded.
  • To create a virtualenv, simply invoke virtualenv ENV, where ENV is is a directory to place the new virtual environment.
  • To initialize the virtualenv, you need to source ENV/bin/activate. To stop using, simply call deactivate.
  • Once you activate the virtualenv, you might install all of a workspace’s package requirements by running pip install -r against the project’s requirements.txt file.

Anaconda: Package Manager + Environment Manager + Additional Scientific Libraries.

  • From the docs: Anaconda 4.2.0 includes an easy installation of Python (2.7.12, 3.4.5, and/or 3.5.2) and updates of over 100 pre-built and tested scientific and analytic Python packages that include NumPy, Pandas, SciPy, Matplotlib, and IPython, with over 620 more packages available via a simple conda install <packagename>
  • As a web developer, I haven’t used Anaconda. It’s ~3GB including all the packages.
  • There is a slimmed down miniconda version, which seems like it could be a more simple option than using pip + virtualenv, although I don’t have experience using it personally.
  • While conda allows you to install packages, these packages are separate than PyPI packages, so you may still need to use pip additionally depending on the types of packages you need to install.

See also:


回答 1

简单类比:

  • pyenv〜rbenv
  • 点〜捆绑器
  • 虚拟环境〜rvm中的gemset。可以由捆绑器直接管理,而无需使用gemset。

由于我使用python3,因此我更喜欢名为venv的python3内置虚拟环境。venv简单易用。我建议您阅读其官方文档。该文档简短明了。

在ruby中,我们实际上并不需要虚拟环境,因为捆绑程序会处理它。虚拟环境和捆绑程序都很棒,但是它们的概念不同,但是它们试图解决相同的问题。

Simple analogy:

  • pyenv ~ rbenv
  • pip ~ bundler
  • virtual env ~ gemset in rvm. This can be managed by bundler directly without gemset.

Since I use python3 I prefer the python3 built-in virtual environment named venv. venv is simple and easy to use. I would recommend you to read its official docs. The doc is short and concise.

In ruby, we don’t really need a virtual environment because the bundler takes care of it. Both virtual env and bundler are great, however, they have different solutions to solve the same problem.


venv,pyvenv,pyenv,virtualenv,virtualenvwrapper,pipenv等有什么区别?

问题:venv,pyvenv,pyenv,virtualenv,virtualenvwrapper,pipenv等有什么区别?

Python 3.3在其标准库中包含了新软件包venv。它有什么作用?与似乎与regex匹配的所有其他软件包(py)?(v|virtual|pip)?env有何不同?

Python 3.3 includes in its standard library the new package 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?


回答 0

PyPI软件包不在标准库中:

  • virtualenv是一个非常流行的工具,可为Python库创建隔离的Python环境。如果您不熟悉此工具,我强烈建议您学习它,因为它是非常有用的工具,在本答案的其余部分中,我将对其进行比较。

    它的工作方式是在目录(例如:)中安装一堆文件env/,然后修改PATH环境变量以在其之前添加自定义bin目录(例如:)env/bin/。在完全相同的副本pythonpython3二进制文件放在这个目录中,但是Python编程寻找相对于其路径优先库,环境中的目录。它不是Python标准库的一部分,但是受到PyPA(Python包装管理局)的正式认可。激活后,您可以使用在虚拟环境中安装软件包pip

  • pyenv用于隔离Python版本。例如,您可能想针对Python 2.7、3.6、3.7和3.8测试代码,因此需要一种在它们之间切换的方法。一旦被激活,它的前缀PATH与环境变量~/.pyenv/shims,那里有专用的文件相匹配的Python命令(pythonpip)。这些不是Python附带命令的副本。它们是特殊的脚本,它们可以根据PYENV_VERSION环境变量,.python-version文件或~/.pyenv/version文件即时确定要运行哪个版本的Python 。pyenv使用命令,还可以简化下载和安装多个Python版本的过程pyenv install

  • pyenv-virtualenv是一个插件pyenv由同一作者的pyenv,允许你使用pyenvvirtualenv在同一时间方便。但是,如果您使用的是Python 3.3或更高版本,请pyenv-virtualenv尝试运行python -m venv它(如果有),而不是virtualenv。如果您不希望使用便利功能,则可以在不使用的情况下一起使用virtualenv和。pyenvpyenv-virtualenv

  • virtualenvwrappervirtualenv(参见docs)的一组扩展。它为您提供诸如mkvirtualenv,的命令,lssitepackages尤其是workon在不同virtualenv目录之间切换时。如果您需要多个virtualenv目录,此工具特别有用。

  • pyenv-virtualenvwrapperpyenv与作者相同的插件pyenv,可以方便地集成virtualenvwrapperpyenv

  • pipenv旨在结合Pipfilepipvirtualenv为在命令行一个命令。该virtualenv目录通常放置在中~/.local/share/virtualenvs/XXXXXX是项目目录路径的哈希值。这与不同virtualenv,后者的目录通常位于当前工作目录中。pipenv是指在开发Python应用程序(而不是库)时使用。还有的替代品pipenv,例如poetry,我将不在此处列出,因为该问题仅与名称相似的软件包有关。

标准库:

  • pyvenv是Python 3附带的脚本,但由于存在问题(更不用说混乱的名称了)而在Python 3.6中不推荐使用。在Python 3.6及更高版本中,确切的等效项是python3 -m venv

  • venv是Python 3附带的软件包,您可以使用它运行python3 -m venv(尽管出于某些原因,某些发行版将其分成了单独的发行版软件包,例如python3-venv在Ubuntu / Debian上)。它的作用与相同virtualenv,但仅具有部分功能(请参见此处的比较)。virtualenv继续比受欢迎venv,尤其是因为前者同时支持Python 2和3。

给初学者的建议:

这是我对初学者的个人建议:首先学习virtualenvpip,这些工具可在各种情况下与Python 2和3一起使用,并在需要时选择其他工具。

PyPI packages not in the standard library:

  • virtualenv is a very popular tool that creates isolated Python environments for Python libraries. 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/). 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. It’s not part of Python’s standard library, but is officially blessed by the PyPA (Python Packaging Authority). Once activated, you can install packages in the virtual environment using pip.

  • pyenv is used to isolate Python versions. For example, you may want to test your code against Python 2.7, 3.6, 3.7 and 3.8, so you’ll need a way to switch between them. Once activated, it prefixes the PATH environment variable with ~/.pyenv/shims, where there are special files matching the Python commands (python, pip). These are not copies of the Python-shipped commands; 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 also makes the process of downloading and installing multiple Python versions easier, using the command pyenv install.

  • 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. 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. You can use virtualenv and pyenv together without pyenv-virtualenv, if you don’t want the convenience features.

  • virtualenvwrapper is a set of extensions to virtualenv (see docs). It gives you commands like mkvirtualenv, lssitepackages, and especially workon for switching between different virtualenv directories. This tool is especially useful if you want multiple virtualenv directories.

  • pyenv-virtualenvwrapper is a plugin for pyenv by the same author as pyenv, to conveniently integrate virtualenvwrapper into pyenv.

  • pipenv aims to combine Pipfile, pip and virtualenv into one command on the command-line. The virtualenv directory typically gets placed in ~/.local/share/virtualenvs/XXX, with XXX being a hash of the path of the project directory. This is different from virtualenv, where the directory is typically in the current working directory. pipenv is meant to be used when developing Python applications (as opposed to libraries). There are alternatives to pipenv, such as poetry, which I won’t list here since this question is only about the packages that are similarly named.

Standard library:

  • pyvenv is a script shipped with Python 3 but deprecated in Python 3.6 as it had problems (not to mention the confusing name). In Python 3.6+, the exact equivalent is 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). It serves the same purpose as virtualenv, but only has a subset of its features (see a comparison here). virtualenv continues to be more popular than venv, especially since the former supports both Python 2 and 3.

Recommendation for beginners:

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 other tools once you start needing them.


回答 1

我只是避免virtualenv在Python3.3 +之后使用,而是使用标准附带的库venv。要创建新的虚拟环境,请输入:

$ python3 -m venv <MYVENV>  

virtualenv尝试将Python二进制文件复制到虚拟环境的bin目录中。但是,它不会更新嵌入到该二进制文件中的库文件链接,因此,如果您将Python从源代码构建到具有相对路径名的非系统目录中,则Python二进制文件会中断。由于这是使副本可分发的Python的方式,因此这是一个很大的缺陷。BTW使用来检查OS X上的嵌入式库文件链接otool。例如,在您的虚拟环境中,键入:

$ 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)

因此,我会避免virtualenvwrapperpipenvpyvenv不推荐使用。pyenv似乎是经常使用的地方virtualenv使用,但我会远离它也因为我觉得venv还做什么pyenv是对建。

venv使用用户可安装的库在外壳中创建新的沙盒化的虚拟环境,并且它是多Python安全的新鲜的,因为虚拟环境只能用标准库启动船舶与Python,你必须与各地重新安装任何其他库,而虚拟环境是积极的。沙盒化,因为在虚拟环境外部看不到这些新库安装,因此您可以删除整个环境并重新启动,而不必担心会影响基本的python安装。用户可安装的库,因为创建虚拟环境的目标文件夹时没有pip installsudo在您已经拥有的某个目录中,因此您不需要sudo权限就可以在其中安装库。最终,它是多python安全的,因为在激活虚拟环境时,shell仅看到用于构建该虚拟环境的python版本(3.4、3.5等)。

pyenv类似于venv,它可以让您管理多个python环境。但是,pyenv由于无法方便地将库安装回滚到某些开始状态,因此您admin有时可能需要特权来更新库。所以我认为也最好使用venv

在过去的两年中,我发现了构建系统中的许多问题(emacs软件包,python独立应用程序构建器,安装程序…),最终归结为virtualenv。我认为当我们取消此附加选项并仅使用时,python将是一个更好的平台venv

I would just avoid the use of virtualenv after Python3.3+ and instead use the standard shipped library venv. To create a new virtual environment you would type:

$ python3 -m venv <MYVENV>  

virtualenv tries to copy the Python binary into the virtual environment’s bin directory. 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. Since this is how you make a copy distributable Python, it is a big flaw. BTW to inspect embedded library file links on OS X, use 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. pyvenv is deprecated. 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.

venv creates virtual environments in the shell that are fresh and sandboxed, with user-installable libraries, and it’s multi-python safe. 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. 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. 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. 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.

pyenv is similar to venv in that it lets you manage multiple python environments. 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. So I think it is also best to use 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. I think python will be a better platform when we eliminate this additional option and only use venv.


回答 2

我掉进了pipenv兔子洞(的确是个黑洞和黑洞……),因为最后一个答案是两年多以前的,所以觉得有必要用有关Python虚拟信封主题的最新进展来更新讨论非常有用。找到了。

免责声明:

这个答案是不是对继续有关的优点的激烈参数pipenv VENV如信封解决方案- 我并没有任代言。这是关于PyPA赞同冲突的标准,以及如何未来发展的virtualenv承诺否定制造要么/或它们之间选择的话。我专注于这两个工具正是因为它们是PyPA的受膏工具。

静脉

如OP所述,venv是用于虚拟化环境的工具。不是第三方解决方案,而是本机工具。PyPA认可venv用于创建虚拟信封:“ 在3.5版中进行了更改:现在建议使用venv创建虚拟环境 ”。

吹牛

pipenv- venv-可以用于创建虚拟信封,但是还可以引入包管理和漏洞检查功能。通过使用 Pipfile交付软件包管理requirements.txt,而不是使用。当 PyPA认可pipenv用于包管理时,这似乎意味着取代了。pipenvpipfilerequirements.txt

但是pipenv使用virtualenv作为创建虚拟信封的工具,而不是 venvPyPA认可它为创建虚拟信封的必备工具。

标准冲突:

因此,如果解决虚拟信封解决方案还不够困难,那么我们现在让PyPA认可使用不同虚拟信封解决方案的两个不同工具。Github关于venv vs virtualenv的激烈辩论可以在这里找到该冲突的重点。

解决冲突:

上面链接中提到的Github辩论已经引导了virtualenv的开发,以适应将来的发行版中venv发展

首选内置venv:如果目标python拥有venv,我们将使用该环境创建环境(然后对其进行后续操作以促进我们提供的其他保证)

结论:

因此,看起来这两个相互竞争的虚拟信封解决方案之间将会有一些未来的融合,但是截至目前,pipenv(使用的)virtualenv与有所不同venv

鉴于pipenv解决的问题以及PyPA给予的祝福,它似乎拥有光明的前景。而且,如果virtualenv实现了其建议的开发目标,那么选择pipenvvenv不再是选择虚拟信封解决方案的理由

I’ve went down the pipenv rabbit hole (it’s a deep and dark hole indeed…) and since the last answer is over 2 years ago, felt it was useful to update the discussion with the latest developments on the Python virtual envelopes topic I’ve found.

DISCLAIMER:

This answer is NOT about continuing the raging debate about the merits of pipenv versus venv as envelope solutions- I make no endorsement of either. It’s about PyPA endorsing conflicting standards and how future development of virtualenv promises to negate making an either/or choice between them at all. I focused on these two tools precisely because they are the anointed ones by PyPA.

venv

As the OP notes, venv is a tool for virtualizing environments. NOT a third party solution, but native tool. PyPA endorses venv for creating VIRTUAL ENVELOPES: “Changed in version 3.5: The use of venv is now recommended for creating virtual environments“.

pipenv

pipenv– like venv – can be used to create virtual envelopes but additionally rolls-in package management and vulnerability checking functionality. Instead of using requirements.txt, pipenv delivers package management via Pipfile. As PyPA endorses pipenv for PACKAGE MANAGEMENT, that would seem to imply pipfile is to supplant requirements.txt.

HOWEVER: pipenv uses virtualenv as its tool for creating virtual envelopes, NOT venv which is endorsed by PyPA as the go-to tool for creating virtual envelopes.

Conflicting Standards:

So if settling on a virtual envelope solution wasn’t difficult enough, we now have PyPA endorsing two different tools which use different virtual envelope solutions. The raging Github debate on venv vs virtualenv which highlights this conflict can be found here.

Conflict Resolution:

The Github debate referenced in above link has steered virtualenv development in the direction of accommodating venv in future releases:

prefer built-in venv: if the target python has venv we’ll create the environment using that (and then perform subsequent operations on that to facilitate other guarantees we offer)

Conclusion:

So it looks like there will be some future convergence between the two rival virtual envelope solutions, but as of now pipenv– which uses virtualenv – varies materially from venv.

Given the problems pipenv solves and the fact that PyPA has given its blessing, it appears to have a bright future. And if virtualenv delivers on its proposed development objectives, choosing a virtual envelope solution should no longer be a case of either pipenv OR venv.


回答 3

2020年4月更新

当我看到这篇文章时,我正在寻找相同的内容。我认为对于像我这样的新Python用户而言,使用什么工具这个问题非常令人困惑和困难。这直接来自PyPA网站上关于pipenv的信息:

虽然本教程将pipenv项目作为工具主要集中在Python应用程序开发而不是Python库开发上,但该项目本身目前正在解决多个流程和维护问题,这些问题阻止了bug修复和新功能的发布(整个2019年过去了,而没有新版本)。这意味着,在短期内,pipenv仍然会遇到一些怪癖和性能问题,而没有明确解决这些问题的时间表。

尽管情况仍然如此,但项目维护人员可能希望研究其他用于应用程序依赖性管理的工具,以代替pipenv或与之一起使用。

假设2020年4月发布的pipenv按计划进行,此后的发布也仍在进行中,那么该教程中的警告将被删除。如果这些发行版不符合要求,那么教程本身将被删除,并替换为可用的依赖项管理选项上的讨论页。

April 2020 Update

I was searching for same when I came across this post. I think this issue of what tool to use is quite confusing and difficult for new Python users like me. This is directly from PyPA website regarding pipenv:

While this tutorial covers the pipenv project as a tool that focuses primarily on the needs of Python application development rather than Python library development, the project itself is currently working through several process and maintenance issues that are preventing bug fixes and new features from being published (with the entirety of 2019 passing without a new release). This means that in the near term, pipenv still suffers from several quirks and performance problems without a clear timeline for resolution of those isses.

While this remains the case, project maintainers are likely to want to investigate Other Tools for Application Dependency Management for use instead of, or together with, pipenv.

Assuming the April 2020 pipenv release goes ahead as planned, and the release after that also remains on track, then this caveat on the tutorial will be removed. If those releases don’t remain on track, then the tutorial itself will be removed, and replaced with a discussion page on the available dependency management options.