python pyenv
Whether you’re just getting started or you’re a seasoned Python developer, you may have found managing your Python environments to be tedious and painful. Managing Python versions, libraries, and various dependencies is like playing shuffleboard where the object is to not hit any other puck; if you do, the probability of a cascading effect of pucks flying everywhere you don’t want them to be will soon follow.
无论您是刚刚起步还是是经验丰富的Python开发人员,您都可能发现管理Python环境非常繁琐和痛苦。 管理Python版本,库和各种依赖项,就像玩沙狐球一样,不要让对象碰到其他冰球。 如果您这样做了,很快就会出现冰球连锁React的可能性。
In this tutorial, you’ll install pyenv for managing python environments, install direnv to auto configure and source the virtualenv for projects, set a global Python version and a local Python version for projects, and configure a virtualenv and auto source the venv when moving into your project directories. By the end of this tutorial, you will be able to install any valid version of python, set up and configure virtual environments for each project, and bring sanity from chaos.
在本教程中,您将安装pyenv来管理python环境,安装direnv来为项目自动配置和获取virtualenv,为项目设置全局Python版本和本地Python版本,并配置virtualenv并在移动时自动为venv提供源到您的项目目录中。 在本教程结束时,您将能够安装任何有效版本的python,为每个项目设置和配置虚拟环境,并避免混乱。
We’re going to install both pyenv
and direnv
via homebrew
to ease the install process. Before getting started, install homebrew if you don’t have it already.
我们将通过homebrew
安装pyenv
和direnv
,以简化安装过程。 在开始之前,请安装Homebrew(如果尚未安装)。
To start, you’ll see how to work with pyenv.
首先,您将了解如何使用pyenv。
To get things started, we’re going to install pyenv
with homebrew and add the required pyenv
init to our ~/.bashrc
file.
首先,我们将使用自制软件安装pyenv
并将所需的pyenv
init添加到我们的~/.bashrc
文件中。
$ brew install pyenv
$ echo 'eval "$(pyenv init -)"' >> ~/.bashrc # initialize pyenv on new shells
$ source ~/.bashrc # reinitialize bashrc to reflect changes in your current shell
Once it’s installed, take a moment to examine what our environment looks like. On a fresh system, you’ll see something similar to this:
安装完成后,请花点时间检查一下我们的环境。 在全新的系统上,您将看到类似以下内容:
$ which pyenv
/usr/local/bin/pyenv
$ pyenv versions
* system
$ which pip
/usr/local/bin/pip
$ which python
/usr/local/bin/python
This is a pretty standard snapshot. When you execute python version
, you’ll notice * system
, as the reference implies, this is your system’s python version. The asterisk denotes the current python binary sourced in your ${PATH}
. A good rule of thumb is to leave the system binary alone. If you want to see the list of python versions that pyenv
can install for you, use pyenv install --list
.
这是一个非常标准的快照。 当执行python version
,您会注意到* system
,正如参考文献所暗示的,这是系统的python版本。 星号表示来自${PATH}
的当前python二进制文件。 一个好的经验法则是不让系统二进制文件。 如果要查看pyenv
可以为您安装的python版本的列表,请使用pyenv install --list
。
$ pyenv install 2.7.15
$ pyenv install 3.7.0
$ pyenv versions
* system (set by /Users/iamjohnnym/.pyenv/version)
2.7.15
3.7.0
Now, let’s upgrade pip
since chances are, it installed an older version. The following command will loop through your installed versions and update pip
to the latest.
现在,让我们升级pip
因为它安装了较旧的版本。 以下命令将循环浏览已安装的版本,并将pip
更新为最新版本。
for VERSION in $(pyenv versions --bare) ; do
pyenv shell ${VERSION} ;
pip install --upgrade pip ;
done
For the sake of our desired workflow, we want to install py2venv
for our Python 2.x
versions so we can mimic how python 3.x
installs virtualenvs. python -m venv .venv
.
为了实现所需的工作流程,我们想为Python 2.x
版本安装py2venv
,以便我们可以模仿python 3.x
如何安装virtualenvs。 python -m venv .venv
。
for VERSION in $(pyenv versions --bare | egrep '^2.') ; do
pyenv shell ${VERSION} ;
pip install py2venv ;
done
Even though we have pyenv
installed, it will still default to system
. To change this, set python 3.7.0
as our global version:
即使我们安装了pyenv
,它仍然默认为system
。 要更改此设置,请将python 3.7.0
设置为我们的全局版本:
$ pyenv global 3.7.0
$ pyenv versions
system
2.7.15
* 3.7.0 (set by /Users/iamjohnnym/.pyenv/version)
$ which python
/Users/iamjohnnym/.pyenv/shims/python
We’ve got pyenv
setup and functional. Let’s move on to direnv
.
我们已经有了pyenv
设置和功能。 让我们继续前进到direnv
。
direnv
is a handy utility that allows you to create a file that’s placed in any directory that you want that functions like .bashrc
. Whenever you enter the directory with this file, your shell will automatically execute it. The capabilities are endless but for the purpose of this post, we’re going to use it to configure a python virtualenv based on the file .python-version
and then activate it for us. The purpose is to create a seamless development flow. No need to worry about manually configuring or activating virtualenvs; let your computer do the work for you.
direnv
是一个方便的实用程序,可让您创建一个文件,该文件放置在您想要该功能的任何目录中,例如.bashrc
。 每当您使用此文件进入目录时,您的外壳程序都会自动执行它。 这些功能是无止境的,但出于本文的目的,我们将使用它基于.python-version
文件配置python virtualenv,然后为我们激活它。 目的是创建一个无缝的开发流程。 无需担心手动配置或激活虚拟环境; 让您的计算机为您完成工作。
Installation is straightforward with homebrew:
使用自制程序安装非常简单:
$ brew install direnv
direnv
is now installed, and ready for exploitation. Let’s try a sample project.
现在已安装direnv
,并准备进行开发。 让我们尝试一个示例项目。
Let’s start up a project. We’re going to create a new directory, set a local python version, set up our .envrc
file, and activate it.
让我们开始一个项目。 我们将创建一个新目录,设置一个本地python版本,设置我们的.envrc
文件并激活它。
$ cd -p ~/python-projects/pyenv-tutorial
$ cd $_ # if you're unaware, $_ will execute the last argument of your command
$ pwd
/Users/iamjohnnym/ python-projects/pyenv-tutorial
$ pyenv local 3.7.0
$ cat .python-version
3.7.0
At this point, we’re ready to create our .envrc
file. With your favorite editor, create that file and add the following contents:
至此,我们准备创建.envrc
文件。 使用您最喜欢的编辑器,创建该文件并添加以下内容:
# check if python version is set in current dir
if [ -f ".python-version" ] ; then
if [ ! -d ".venv" ] ; then
echo "Installing virtualenv for $(python -V)"
# if we didn't install `py2venv` for python 2.x, we would need to use
# `virtualenv`, which you would have to install separately.
python -m venv .venv
fi
echo "Activating $(python -V) virtualenv"
source .venv/bin/activate
fi
# announce python version and show the path of the current python in ${PATH}
echo "Virtualenv has been activated for $(python -V)"
echo "$(which python)"
Save the file. If you did this via a shell editor, such as vim
. You’ll see the following message, direnv: error .envrc is blocked. Run direnv allow to approve its content
. Don’t be alarmed as this is a security feature to prevent auto-execution of the file. Whenever this file is changed, it requires manual approval before it will auto-execute again. To activate it, simply type direnv allow
from the project dir.
保存文件。 如果通过外壳编辑器(例如vim
。 您将看到以下消息direnv: error .envrc is blocked. Run direnv allow to approve its content
direnv: error .envrc is blocked. Run direnv allow to approve its content
。 请勿惊慌,因为这是防止文件自动执行的安全功能。 无论何时更改此文件,都需要手动批准,然后它将再次自动执行。 要激活它,只需在项目目录中键入direnv allow
。
$ direnv allow
direnv: loading .envrc
Installing virtualenv for Python 3.7.0
Activating Python 3.7.0 virtualenv
Virtualenv has been activated for Python 3.7.0
/Users/iamjohnnym/.personal/tutorials/pyenv-direnv/.venv/bin/python
direnv: export +VIRTUAL_ENV ~PATH
You now have the tools you need to manage different python versions and project dependencies!
现在,您有了管理不同的python版本和项目依赖项所需的工具!
翻译自: https://www.digitalocean.com/community/tutorials/how-to-manage-python-with-pyenv-and-direnv
python pyenv