如何管理多个Python版本和虚拟环境
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 pip
将pip
升级到最新版本。
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版本:
The
PYENV_VERSION
environment variable (if specified). You can use thepyenv shell
command to set this environment variable in your current shell session.PYENV_VERSION
环境变量(如果指定)。 您可以使用pyenv shell
命令在当前的shell会话中设置此环境变量。The application-specific
.python-version
file in the current directory (if present). You can modify the current directory's.python-version
file with thepyenv local
command.当前目录(如果存在)中特定
.python-version
应用程序的.python-version
文件。 您可以使用pyenv local
命令修改当前目录的.python-version
文件。The first
.python-version
file found (if any) by searching each parent directory, until reaching the root of your filesystem.通过搜索每个父目录找到第一个
.python-version
文件(如果有),直到到达文件系统的根目录。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 yourPATH
.)全局版本文件。 您可以使用
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是可以接受的,那么我们也可以选择使用python3
在python3.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/
相关文章:

linux 下byte,char,unsigned char的区别
在linux中,对byte的定义为无符号char,而char默认为有符号char。 #ifndef BYTE #define BYTE unsigned char #endif以下ZZ百度知道: 在C中,默认的基础数据类型均为signed,现在我们以char为例,说明(signed) c…

词法作用域和动态作用域
JavaScript采用的是词法作用域 1.词法作用域 即函数定义时,即确定的作用域。js中的作用域链,在函数声明时候,就已经确定了,无论函数在何处调用,其作用域变量的查找都是按照定义是包含关系去查找。 2.动态作用域 变量的…

10-18 JS基础复习笔记
微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 1.JS类型 Numbel String Boolean Symbol Null Undefined Object(Funtion,Array,Data,Regexp); 2.字符串转数字类型 "122" //122 var a 1 2; console.log(a) //3 3.null 和 u…

vue.js crud_如何使用VS Code和ADO.NET使用ASP.NET Core执行CRUD操作
vue.js crud介绍 (Introduction) In this article we are going to create a web application using ASP.NET Core MVC with the help of Visual Studio Code and ADO.NET. We will be creating a sample Employee Record Management System and performing CRUD operations on…
redis事物命令示例
命令示例: 1. 事务被正常执行:#在Shell命令行下执行Redis的客户端工具。/> redis-cli#在当前连接上启动一个新的事务。redis 127.0.0.1:6379>multiOK#执行事务中的第一条命令,从该命令的返回结果可以看出,该命令并没有立即执…

JS 函数 函数递归
微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 重要:函数也是对象,你可以给它们添加属性或者更改它们的属性。 函数内部对象:arguments 解析:函数实际上是访问了函数体中一个名为 arguments 的内部对象…

swift设置启动图不现实_如何通过装饰房屋来开始在Swift中使用增强现实
swift设置启动图不现实by Ranadhir Dey由Ranadhir Dey 如何通过装饰房屋来开始在Swift中使用增强现实 (How to get started with augmented reality in Swift by decorating your home) If you’ve read my previous post, you already have a beautiful AR floor in your din…

可用于nodejs的SuperAgent(ajax API)
简单示例: import request from superagent;//引用声明 request.post(api).withCredentials()//跨域.end((err, res) > {if (res.ok) {const json JSON.parse(res.text);} else {console.log(获取失败);}}); 1、get 方式 当使用get请求传递查询字符串的时候&…

Java第四次实验
一、实验内容: 结对编程。运行TCP代码,结对进行,一人服务器,一人客户端;利用加解密代码包,编译运行代码,一人加密,一人解密; 集成代码,密后通过TCP发送。 二、…

JS 面向对象编程之原型(prototype)
微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 function Person(first, last) {this.first first;this.last last; } Person.prototype.fullName function() {return this.first this.last; } Person.prototype.fullNameReversed function() {…

idea自动捕获_Smilefie:如何通过检测微笑来自动捕获自拍
idea自动捕获by Rishav Agarwal通过里沙夫阿加瓦尔 Smilefie:如何通过检测微笑来自动捕获自拍 (Smilefie: how you can auto-capture selfies by detecting a smile) Ten second takeaway: use Python and OpenCV to create an app that automatically captures a …

hiho 1015 KMP算法 CF 625 B. War of the Corporations
#1015 : KMP算法 时间限制:1000ms单点时限:1000ms内存限制:256MB描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编程的学习道路上一同前进。 这一天,他们遇到了一只河蟹&#…

React 组件绑定点击事件,并且传参完整Demo
微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 1.传数组下标给点击事件Demo: const A 65 // ASCII character codeclass Alphabet extends React.Component {constructor(props) {super(props);this.handleClick this.handleClick.bind…

ScaleYViewPager
https://github.com/eltld/ScaleYViewPager 转载于:https://www.cnblogs.com/eustoma/p/4572925.html

node mongoose_如何使用Express,Mongoose和Socket.io在Node.js中构建实时聊天应用程序
node mongooseby Arun Mathew Kurian通过阿伦马修库里安(Arun Mathew Kurian) 如何使用Express,Mongoose和Socket.io在Node.js中构建实时聊天应用程序 (How to build a real time chat application in Node.js using Express, Mongoose and Socket.io) In this tut…

结对项目开发电梯调度 - 整体设计
一、系统介绍 1. 功能描述 本电梯系统用来控制一台运行于一个具有16层的大楼电梯,它具有上升、下降、开门、关门、载客的基本功能。 大楼的每一层都有: (1) 两个指示灯: 这两个指示灯分别用于指示当前所在的层数和电梯的当前状态ÿ…

3.分支结构与循环结构
1 程序结构 程序结构分为顺序结构、分支结构、循环结构。分支结构有:if结构,if....else结构,if...else if....else ,if...else结构,switch结构;循环结构有:while循环,do....while循环…

微信小程序 实现复制到剪贴版功能
微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 实现API: wx.setClipboardData(Object object) API说明:设置系统剪贴板的内容 属性类型默认值是否必填说明支持版本datastring 是剪贴板的内容 successfunction 否接口调用成功…

数据结构面试题编程题_您下次编程面试时应该了解的顶级数据结构
数据结构面试题编程题by Fahim ul Haq通过Fahim ul Haq Niklaus Wirth, a Swiss computer scientist, wrote a book in 1976 titled Algorithms Data Structures Programs.瑞士计算机科学家Niklaus Wirth在1976年写了一本书,名为《 算法数据结构程序》。 40 yea…

oracle使用小技巧
批量禁用触发器 SELECT ALTER TRIGGER || trigger_name || DISABLE; FROM all_triggers; 这样就生成了一个禁用语句列表,复制到sql脚本执行界面,批量执行即可,类似的,可以用all_tables来批量删除表。 转载于:https://www.cnblogs…

if for switch语句
顺序语句:一行行执行条件语句:选择分支if语句 1、 if(....)//括号内是判断条件 { //程序代码,运算等等 } 2、 if(....)//括号内是判断条件 { //程序代码,运算等等 } else//如果不满足条件则执…

Apache Unable to find the wrapper https - did you forget to enable it when you configured PHP?
微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 Apache Unable to find the wrapper "https" - did you forget to enable it when you configured PHP? 问题解决办法: 打开配置文件 php.ini , 如图: …

文件魔术数字_如何使用魔术脚手架自动创建文件并节省时间
文件魔术数字Before we begin: This article uses JavaScript / Node.js example code, but you can port these concepts to any language using the right tools.开始之前:本文使用JavaScript / Node.js示例代码,但是您可以使用正确的工具将这些概念移…

Sql Server统计报表案例
场景:查询人员指定年月工作量信息 USE [Test] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER procedure [dbo].[GetWorkLoadMain] year int, month int, UserId varchar(50) as begindeclare day varchar(50)set dayCAST(year as varchar)-RIGHT((00…

运行报表时提示输入用户名和密码
在AX2012运行报表是总是提示用户输入用户名和密码: 尝试输入登陆名和密码,点击查看报表,出现如下错误: 因为AX2012的报表使用的针对AX2012客制化的SSRS,而要求输入登录名和密码是SSRS报表的数据源设置导致的。在SSRS管…

CSS 文字,边框实现从左至右颜色渐变
微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 1.文本从左至右颜色渐变 效果图: 2.边框从左至右颜色渐变 效果图: 实现代码: 1.文本从左至右颜色渐变实现代码: <!DOCTYPE html> <html>&l…

如何使用Create-React-App和自定义服务人员构建PWA
Note: This is not a primer on create-react-app or what a service worker is. This post assumes prior knowledge of both.注意:这不是create-react-app或服务工作者的入门。 这篇文章假定两者都有先验知识。 So, I recently had the opportunity to work on a…

inline-block空隙怎么解决
方法一:移除空格 元素间留白间距出现的原因就是标签段之间的空格,因此,去掉HTML中的空格,自然间距就木有了。考虑到代码可读性,显然连成一行的写法是不可取的,我们可以: <div class"spa…

php 网络请求 get请求和post请求
微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 代码记录 <?php header(content-type:application:json;charsetutf8); header(Access-Control-Allow-Origin:*); //header(Access-Control-Allow-Methods:POST); header(Access-Control-Allow-He…

docker查看现有容器_如何使用Docker将现有应用程序推送到容器中
docker查看现有容器by Daniel Newton丹尼尔牛顿 如何使用Docker将现有应用程序推送到容器中 (How to shove an existing application into containers with Docker) I have finally got round to learning how to use Docker past the level of knowing what it is and does w…