博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python pyenv_如何使用Pyenv和Direnv管理Python
阅读量:2507 次
发布时间:2019-05-11

本文共 7482 字,大约阅读时间需要 24 分钟。

python pyenv

介绍 (Introduction)

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 for managing python environments, install 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.

在本教程中,您将安装来管理python环境,安装来为项目自动配置和获取virtualenv,为项目设置全局Python版本和本地Python版本,并配置virtualenv并在移动时自动为venv提供源到您的项目目录中。 在本教程结束时,您将能够安装任何有效版本的python,为每个项目设置和配置虚拟环境,并避免混乱。

先决条件 (Prerequisites)

We’re going to install both pyenv and direnv via homebrew to ease the install process. Before getting started, install if you don’t have it already.

我们将通过homebrew安装pyenvdirenv ,以简化安装过程。 在开始之前,请安装如果尚未安装)。

使用pyenv (Working with pyenv)

To start, you’ll see how to work with pyenv.

首先,您将了解如何使用pyenv。

安装pyenv (Installing 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

配置基本要求 (Configuring the Base Requirements)

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

设置全局Python版本 (Setting the Global Python Version)

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合作 (Working with 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,然后为我们激活它。 目的是创建一个无缝的开发流程。 无需担心手动配置或激活虚拟环境; 让您的计算机为您完成工作。

安装direnv (Installing direnv)

Installation is straightforward with homebrew:

使用自制程序安装非常简单:

$ brew install direnv

direnv is now installed, and ready for exploitation. Let’s try a sample project.

现在已安装direnv ,并准备进行开发。 让我们尝试一个示例项目。

创建一个新项目 (Creating a New Project)

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-version3.7.0

配置环境文件 (Configuring the Environment File)

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 dirif [ -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/activatefi# 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 allowdirenv: loading .envrcInstalling virtualenv for Python 3.7.0Activating Python 3.7.0 virtualenvVirtualenv has been activated for Python 3.7.0/Users/iamjohnnym/.personal/tutorials/pyenv-direnv/.venv/bin/pythondirenv: export +VIRTUAL_ENV ~PATH

结论 (Conclusion)

You now have the tools you need to manage different python versions and project dependencies!

现在,您有了管理不同的python版本和项目依赖项所需的工具!

翻译自:

python pyenv

转载地址:http://qohgb.baihongyu.com/

你可能感兴趣的文章
定时Job在IIS中潜在危险-IIS 定期回收
查看>>
Kafka的安装和配置
查看>>
Alpha冲刺(10/10)
查看>>
数组Array的API2
查看>>
为什么 Redis 重启后没有正确恢复之前的内存数据
查看>>
No qualifying bean of type available问题修复
查看>>
第四周助教心得体会
查看>>
spfile
查看>>
Team Foundation Service更新:改善了导航和项目状态速查功能
查看>>
0x13 链表与邻接表
查看>>
js封装设置获取cookie
查看>>
二值图像连通区域标记
查看>>
MVC in Javascript
查看>>
eclipse 创建的Android工程的结构
查看>>
第8章 Android异常与性能优化相关面试问题
查看>>
有道单词导入 大量有道单词 生词本 批量导入 添加 有道单词XML 背单词
查看>>
jQuery Easing动画效果扩展插件
查看>>
bzoj 1002 [FJOI2007]轮状病毒 Matrix-Tree定理+递推
查看>>
Selenium WebDriver- 操作JavaScript的Alert弹窗
查看>>
娘的,自己的求逆序对模板又不好使了。。。。。。。。
查看>>