Addition January 2019: If you are coming back to this blog after upgrading to macOS Mojave please see this github issue for a solution to the common pyenv ‘zlib not available’ problem.

此外,2019年1月:如果在升级到macOS Mojave之后又回到此博客,请参阅此github问题以获取常见pyenv'zlib not available'问题的解决方案。

Before we start, let’s briefly go over the terms used in the title:

在开始之前,让我们简要回顾一下标题中使用的术语:

  • Multiple Python versions: Different installations of Python on the same machine, 2.7 and 3.4 for example.

    多个Python版本 :同一台计算机上的Python安装不同,例如2.7和3.4。

  • Virtual environments: isolated independent environments that can have both a specific version of Python and of any project-specific packages installed within them, without affecting any other projects.

    虚拟环境 :隔离的独立环境,可以在其中安装特定版本的Python和特定于项目的程序包,而不会影响任何其他项目。

Here we’ll look at three different tools for working with these, and when you might need each one. Let’s explore the use cases for:

在这里,我们将研究三种不同的工具来处理这些工具,以及何时需要它们。 让我们探索以下用例:

  • venv / pyvenv

    venv / pyvenv

  • pyenv

    pyenv

  • pyenv-virtualenv

    pyenv-virtualenv

If you are using a single version of Python say version 3.3+, and want to manage different virtual environments, then venv is all you need.

如果您使用的是Python的单个版本 ,例如3.3+ ,并且想要管理不同的虚拟环境,那么venv是您所需要的。

If you want to use multiple versions of Python at 3.3+, with or without virtual environments, then continue to read about pyenv.

如果您想在3.3 以上的Python中使用多个版本无论是否 带有虚拟环境 ,请继续阅读pyenv

If you also want to work with Python 2, then pyenv-virtualenv is a tool to consider.

如果您还想使用Python 2 ,那么pyenv-virtualenv是一个pyenv-virtualenv考虑的工具。

静脉 (venv)

From Python 3.3+ the venv package is included. It is ideal for creating lightweight virtual environments.

从Python 3.3+起,包括venv软件包。 它是创建轻量级虚拟环境的理想选择。

Up until Python 3.6 a script called pyvenv was also included as a wrapper around venv, but this has been deprecated. It will be completely removed in Python 3.8. The exact same functionality is available when using venv, and any existing documentation should be updated. For anyone interested you can read the reasons behind depreciating pyvenv.

在Python 3.6 pyvenv ,还包括一个名为pyvenv的脚本作为venv的包装,但是已弃用了该脚本。 它将在Python 3.8中完全删除。 使用venv ,可以使用完全相同的功能,并且应更新任何现有文档。 对于感兴趣的任何人,您都可以阅读pyvenv贬值的原因 。

venv is used to create a new environment via the terminal command:

venv用于通过terminal命令创建新环境:

$ python3 -m venv directory-name-to-create

activated with:

已激活:

$ source name-given/bin/activate

and deactivated with simply:

并通过以下方式停用:

$ deactivate

If you need to remove the environment completely after deactivating it, you can run:

如果您需要在停用后完全删除环境,则可以运行:

$ rm -r name-given

By default, the environment it creates will be the current version of Python you are using. If you are writing documentation, and want the additional safety that the correct version of Python is being used by your reader you can specify the major and minor version number in the command, like so:

默认情况下,它创建的环境将是您正在使用的Python的当前版本。 如果您正在编写文档,并且希望读者能够使用正确版本的Python来提高安全性,则可以在命令中指定主要和次要版本号,如下所示:

$ python3.6 -m venv example-three-six

If the reader is using a version other than 3.6 then the command will not be successful and will indicate in its error message. However, any patch version (for example 3.6.4) will work.

如果阅读器使用的版本不是3.6,则该命令将不会成功,并且会在错误消息中指出。 但是,任何补丁程序版本(例如3.6.4)都可以使用。

When the environment is active, any packages can be installed to it via pip as normal. By default, the newly created environment will not include any packages already installed on the machine. As pip itself will not necessarily be installed on the machine. It is recommended to first upgrade pip to the latest version, using pip install --upgrade pip.

当环境处于活动状态时,可以正常通过pip将任何软件包安装到该环境。 默认情况下,新创建的环境将包括计算机上已安装的任何软件包。 由于pip本身不一定安装在机器上。 建议先使用pip install --upgrade pippip升级到最新版本。

Projects will commonly have a requirements.txt file specifying its dependencies. This allows the shortcut command pip install -r requirements.txt command to quickly install all packages to the newly created virtual environment. They will only exist in the virtual environment. It will not be available when it is deactivated but will persist when it is reactivated.

项目通常会有一个requirements.txt文件来指定其依赖项。 这使快捷方式命令pip install -r requirements.txt命令可以将所有软件包快速安装到新创建的虚拟环境中。 它们仅存在于虚拟环境中。 禁用后它将不可用,但是重新激活后将保持不变。

If you do not need to use additional versions of Python itself, then this is all you need to create isolated, project specific, virtual environments.

如果您不需要使用其他版本的Python本身,那么这就是创建隔离的,特定于项目的虚拟环境所需的全部。

pyenv (pyenv)

If you wish to use multiple versions of Python on a single machine, then pyenv is a commonly used tool to install and switch between versions. This is not to be confused with the previously mentioned depreciated pyvenv script. It does not come bundled with Python and must be installed separately.

如果您希望在一台计算机上使用多个版本的Python,则pyenv是安装和在版本之间切换的常用工具。 请勿将其与前面提到的已贬值的pyvenv脚本混淆。 它没有与Python捆绑在一起,必须单独安装。

The pyenv documentation includes a great description of how it works, so here we will look simply at how to use it.

pyenv 文档 对它的工作方式进行了很好的描述,因此在这里我们将简单地看一下如何使用它。

Firstly we will need to install it. If using Mac OS X, we can do this using Homebrew, else consider other installation options.

首先,我们需要安装它。 如果使用Mac OS X,我们可以使用Homebrew进行此操作,否则请考虑其他安装选项 。

$ brew update
$ brew install pyenv

Next, add the following towards the bottom of your shell scripts to allow pyenv to automatically change versions for you:

接下来,在您的shell脚本底部添加以下内容,以使pyenv为您自动更改版本:

eval "$(pyenv init -)"

To do, open your in use shell script, via $ ~/.zshrc, $ ~/.bashrc or $ ~/.bash_profile and copy and paste the above line in.

为此,请通过$ ~/.zshrc$ ~/.bashrc$ ~/.bash_profile打开使用中的shell脚本,然后复制并粘贴以上行。

Running pyenv versions will show which Python versions are currently installed, with a * next to the one currently in use. pyenv version shows this directly, and python --version can be used to verify this.

正在运行的pyenv versions将显示当前安装的Python版本,并在当前使用的版本旁边显示*pyenv version直接显示了这一点,并且可以使用python --version进行验证。

To install an additional version, say 3.4.0, simply use pyenv install 3.4.0.

要安装其他版本,例如3.4.0 ,只需使用pyenv install 3.4.0

pyenv looks in four places to decide which version of Python to use, in priority order:

pyenv在四个地方按优先级顺序决定要使用的Python版本:

  1. The PYENV_VERSION environment variable (if specified). You can use the pyenv shell command to set this environment variable in your current shell session.

    PYENV_VERSION环境变量(如果指定)。 您可以使用pyenv shell命令在当前的shell会话中设置此环境变量。

  2. The application-specific .python-version file in the current directory (if present). You can modify the current directory's .python-version file with the pyenv local command.

    当前目录(如果存在)中特定.python-version应用程序的.python-version文件。 您可以使用pyenv local命令修改当前目录的.python-version文件。

  3. The first .python-version file found (if any) by searching each parent directory, until reaching the root of your filesystem.

    通过搜索每个父目录找到第一个.python-version文件(如果有),直到到达文件系统的根目录。

  4. The global version file. You can modify this file using the pyenv global command. If the global version file is not present, pyenv assumes you want to use the "system" Python. (In other words, whatever version would run if pyenv weren't in your PATH.)

    全局版本文件。 您可以使用pyenv global命令修改此文件。 如果不存在全局版本文件,则pyenv假定您要使用“系统” Python。 (换句话说,如果pyenv不在PATH则可以运行任何版本。)

When setting up a new project that is to use Python 3.6.4 then pyenv local 3.6.4 would be ran in its root directory. This would both set the version, and create a .python-version file, so that other contributors’ machines would pick it up.

设置使用Python 3.6.4的新项目时,将在其根目录中运行pyenv local 3.6.4 。 这将设置版本,并创建一个.python-version文件,以便其他贡献者的计算机可以选择它。

The full description of pyenv commands is one to bookmark.

pyenv命令的完整描述很pyenv被收藏。

pyenv和venv (pyenv and venv)

When working with Python 3.3+ we now know both how to install and switch between different versions of Python, and how to create new virtual environments.

现在,当使用Python 3.3+时,我们既知道如何在不同版本的Python之间安装和切换,又知道如何创建新的虚拟环境。

As an example, let’s say we were setting up a project that was to use Python 3.4.

举例来说,假设我们正在建立一个使用Python 3.4的项目。

First we could set our local version using pyenv local 3.4.0.

首先,我们可以使用pyenv local 3.4.0设置本地版本。

If we then ran python3 -m venv example-project a new virtual environment would be set up under example-project, using our locally enabled Python 3.4.0.

如果随后运行python3 -m venv example-project则将使用本地启用的Python 3.4.0在example-project下建立一个新的虚拟环境。

We activate using source example-project/bin/activate and can start working.

我们使用source example-project/bin/activate并且可以开始工作。

Next we could optionally document that a collaborator should use python3.4 -m venv <name>. This means even if a collaborator was not using pyenv the python3.4 command would error if their Python version was not the same major and minor version (3 and 4), as we intended.

接下来,我们可以选择记录协作者应使用python3.4 -m venv <name> 。 这意味着,即使协作者未使用pyenv,如果他们的Python版本与我们预期的主版本和次版本(3和4)不同,则python3.4命令也会出错。

Alternatively, we could choose to simply specify that 3.4.0 was to be used, and instruct python3 -m venv <name>. If we believe that any version greater than 3.4 is acceptable, then we also may choose to use python3 over python3.4, as if the collaborator was using 3.6 then they would otherwise also receive an error. This is a project specific decision.

另外,我们可以选择简单地指定要使用3.4.0,并指示python3 -m venv <name> 。 如果我们相信,任何已经rsion摹 reater比3.4是可以接受的,那么我们也可以选择使用python3python3.4 ,仿佛合作者使用3.6那么他们本来也收到一个错误。 这是项目特定的决定。

pyenv-virtualenv (pyenv-virtualenv)

pyenv can be used to install both Python 2 and 3 versions. However, as we have seen, venv is limited to versions of Python greater than 3.3.

pyenv可用于安装Python 2和3版本。 但是,正如我们所看到的, venv仅限于大于3.3的Python版本。

pyenv-virtualenv is a tool to create virtual environments integrated with pyenv, and works for all versions of Python. It is still recommended to use the official Python venv where possible. But if, for example, you’re creating a virtual environment based on 2.7.13, then this compliments pyenv.

pyenv-virtualenv是用于创建与pyenv集成的虚拟环境的工具,并且适用于所有版本的Python。 仍然建议尽可能使用官方Python venv 。 但是,例如,如果您要基于2.7.13创建一个虚拟环境,则可以称赞pyenv

It also works well with Anaconda and Miniconda conda environments if you are already using those. A tool called virtualenv also exists. It’s not covered here, but it’s linked at the end.

它还与运作良好Python和Miniconda conda ,如果你已经在使用这些环境。 还存在一个称为virtualenv的工具。 它没有在这里讨论,但是在最后链接了。

After installing pyenv it can next be installed using Homebrew (or alternatives) as so:

安装pyenv ,接下来可以使用Homebrew( 或替代品 )进行安装,如下所示:

$ brew install pyenv-virtualenv

Next in your .zshrc, .bashrc, or .bash_profile (depending on which shell you use) add the following towards the bottom:

接下来,在.zshrc.bashrc.bash_profile (取决于您使用的外壳),在底部添加以下内容:

eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

This allows pyenv to activate and deactivate environments automatically when moving directories.

pyenv允许在移动目录时自动激活和停用环境。

To create a new virtual environment, use:

要创建新的虚拟环境,请使用:

$ pyenv virtualenv <version> <name-to-give-it>// for example$ pyenv virtualenv 2.7.10 my-virtual-env-2.7.10

Existing environments can be listed with:

现有环境可以列出:

$ pyenv virtualenvs

Activated/ deactivated with:

通过以下方式激活/停用:

$ pyenv activate <name>
$ pyenv deactivate

At the time of writing, when using activate the warning prompt changing will be removed from future release will be displayed. This is expected and refers only to the (env-name) being displayed in your shell, not the use of the activate command itself.

在撰写本文时,使用activate警告prompt changing will be removed from future release将显示prompt changing will be removed from future release 。 这是预料之中的 ,仅指在外壳程序中显示的(env-name) ,而不是使用activate命令本身。

Installing requirements works as described in venv. Unlike in venv a rm -r command is not needed to remove an environment, an uninstall command exists.

安装要求按venv所述venv 。 与venv不同, venv rm -r命令来删除环境,而存在uninstall 命令 。

最后的想法 (Final thoughts)

Between these three tools, we have the ability to collaborate on any project, no matter the version of Python or of the dependencies required. We also know how to document set up instructions for others to use for any project we work on.

在这三个工具之间,无论Python版本或所需的依赖项如何,我们都可以在任何项目上进行协作。 我们还知道如何记录设置说明,以供其他人在我们正在从事的任何项目中使用。

We can also see the reasoning behind which set to use, as not all developers will require all three.

我们还可以看到要使用该集合的原因,因为并非所有开发人员都需要这三个。

Hopefully this was helpful, and is a useful reference in combination with the documentation linked below.

希望这会有所帮助,并且与下面链接的文档结合在一起是有用的参考。

Thanks for reading! ?

谢谢阅读! ?

我探索过的其他内容: (Other things I’ve explored:)

  • Mocking ES and CommonJS modules with jest.mock()

    使用jest.mock()模拟ES和CommonJS模块

  • A beginner’s guide to Amazon’s Elastic Container Service

    亚马逊弹性容器服务初学者指南

翻译自: https://www.freecodecamp.org/news/manage-multiple-python-versions-and-virtual-environments-venv-pyenv-pyvenv-a29fb00c296f/

如何管理多个Python版本和虚拟环境相关推荐

  1. python多环境管理_怎样管理多个Python版本和虚拟环境

    详解如何管理多个Python版本和虚拟环境 多个Python版本:在同一台机器上安装不同的Python,例如2.7和3.4. 虚拟环境:独立的环境,既可以同时安装特定版本的Python,也可以安装任何 ...

  2. 多版本python共存,安装三方库到指定python版本 多Python版本和虚拟环境

    多个Python版本:在同一台机器上安装不同的Python,例如2.7和3.4. 虚拟环境:独立的环境,既可以同时安装特定版本的Python,也可以安装任何特定于项目的软件包,而不会影响任何其他项目. ...

  3. windows10下使用virtualenv虚拟技术,管理多个python版本,多个项目包环境

    windows10下使用virtualenv虚拟技术,管理多个python解释器版本,多个项目包环境 文章目录 windows10下使用virtualenv虚拟技术,管理多个python解释器版本,多 ...

  4. 虚拟环境工具pyenv的安装,配置和使用,完美控制python版本

    文章目录 前言 背景 使用 第 1 章:使用环境 1.安装 git 2.开启终端 3.安装 pyenv 4.安装 pyenv-virtualenv 第 2 章:使用 pyenv 1. 创建虚拟环境 2 ...

  5. anaconda降级python失败_如何降级Python版本安装spyder?

    如何降级 Python 版本安装 Spyder? 可能并不需要降级 Python 版本,使用 Spyder 独立安装包安装,然后指定 Python 解释器:另外一种方法是在 Anaconda 中安装. ...

  6. Python VScode 配置虚拟环境(virtualenv)

    Python 虚拟环境 Python 之所以强大,除了语言本身的特性外,更重要的是拥有无所不及的第三方库.强大的软件库,让开发者将精力集中在业务上,而避免重复造轮子的浪费.但众多的软件库,形成了复杂的 ...

  7. python antlr4需要的python 版本_python多版本管理器pyenv

    查看Linux版本 1[root@web ~]# cat /etc/redhat-release 2CentOS Linux release 7.6.1810 (Core) 3[root@web ~] ...

  8. 矩池云conda创建指定cuda cudnn tf版本的虚拟环境

    本文将介绍如何在矩池云通过conda创建虚拟环境,并安装指定版本的cuda cudnn tf. 需求:在矩池云中安装一个虚拟环境,python3.6,tensorflow-gpu1.4,cuda8,c ...

  9. python版本切换 for Windows(Anaconda) Linux(pyenv)

    文章目录 一.Windows 使用 Anaconda创建虚拟环境(其它操作系统也可以使用Anaconda完成相同的操作) (一)添加一个所需的python版本的环境(本文以python3.6为例) ( ...

最新文章

  1. python写入数据到excel中_Python写入数据到Excel
  2. 联想笔记本电脑,重新安装系统之U盘启动方法
  3. CentOS7 安装 Gitlab
  4. C++类对象在内存中的布局
  5. springboot最佳实践-SpringBoot应用如何编写
  6. kmeans及模型评估指标_使用sklearn评估器构建聚类模型
  7. 失去老罗,张一鸣的坚果手机多了什么?
  8. 微信农场,第二个QQ秀?
  9. 它利用计算机技术 把企业的物流 人流,ERP原理与沙盘模拟感想
  10. IOS+Android 车牌识别SDK开发包——可以各种角度瞬间OCR识别车牌牌号
  11. 联想ThinkPad笔记本Fn键关闭与启用方法
  12. tar/zip 压缩解压
  13. VMware Horizon 8 2106 -- 用于管理虚拟桌面 (VDI)、应用和在线服务的领先平台
  14. 【干货】Markdown编辑博文,公式图片轻松搞定
  15. Go简明语法汇总--入门
  16. 珍藏资源,msdn2001英文版
  17. 全宇宙第一编辑器VS Code搭配Markdown打造云端笔记(二:MD语法速成)
  18. educoder 国标码转区位码实验(详细)
  19. ISP一键下载电路分析+74HC1G66GW(信号图)
  20. 使用期望等待一次性事件

热门文章

  1. ES6 let和const 命令
  2. spark编程基础--2.3面向对象编程基础
  3. Android架构篇-2 国际化多语言
  4. [微信小程序]计算自己手机到指定位置的距离
  5. swift 和 oc中检测textfield是否输入数字
  6. Node.js的url模块简介
  7. CSS3动画过渡的jquery动态弹出框插件
  8. Linux服务器日志备份到本地
  9. MySQL 代码结构与基本流程
  10. Nutch 二次开发之parse正文内容