如何在 Mac 上为 Python 设置虚拟环境

通过使用 pyenv 和 virtualwrapper 管理虚拟环境,避免很多困惑。
234 位读者喜欢这篇文章。
Using Python to find corrupted images

Jason van Gumster。CC BY-SA 4.0

如果您是一名 Python 开发人员并且是 Mac 用户,那么在新电脑上要做的第一件事就是设置 Python 开发环境。 这是最好的方法(尽管我们已经写过关于在 MacOS 上管理 Python 环境的其他方法)。

准备工作

首先,打开终端并在其冷冰冰的提示符下输入 xcode-select --install。 点击确认,您就可以设置基本开发环境了。 根据 OS X Daily,此步骤在 MacOS 上是必需的,用于设置本地开发实用程序,包括“许多常用工具、实用程序和编译器,包括 make、GCC、clang、perl、svn、git、size、strip、strings、libtool、cpp、what 和许多其他通常在默认 Linux 安装中找到的有用命令”。

接下来,通过从互联网执行以下 Ruby 脚本来安装 Homebrew

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

如果您像我一样,对随意运行来自互联网的脚本存在信任问题,请单击上面的脚本并仔细查看它做了什么。

完成此操作后,恭喜您,您拥有了 Homebrew 中出色的软件包管理工具。 天真地,您可能会认为接下来应该 brew install python 或其他操作。 不,哈哈。 Homebrew 会为您提供一个 Python 版本,但是如果您让该工具为您管理环境,那么您获得的 Python 版本将超出您的控制。 您需要 pyenv,这是一个“用于简单 Python 版本管理的工具”,可以在 许多操作系统上安装。 运行

$ brew install pyenv

您希望每次打开提示符时都运行 pyenv,因此请在您的配置文件中包含以下内容(默认情况下在 MacOS 上,这是您主目录中的 .bash_profile

$ cd ~/
$ echo 'eval "$(pyenv init -)"' >> .bash_profile

通过添加此行,每个新终端都将启动 pyenv 以管理终端中的 PATH 环境变量,并插入您想要运行的 Python 版本(而不是环境中出现的第一个版本。有关更多信息,请阅读“如何在 Linux 中设置 $PATH 变量。”) 打开一个新的终端以使更新后的 .bash_profile 生效。

在安装您喜欢的 Python 版本之前,您需要安装几个有用的工具

$  brew install zlib sqlite

zlib 压缩算法和 SQLite 数据库是 pyenv 的依赖项,并且通常在配置不正确时会导致构建问题。 将这些导出添加到您当前的终端窗口,以确保安装完成

$ export LDFLAGS="-L/usr/local/opt/zlib/lib -L/usr/local/opt/sqlite/lib"
$ export CPPFLAGS="-I/usr/local/opt/zlib/include -I/usr/local/opt/sqlite/include"

既然准备工作已经完成,现在是时候安装适合现代人的 Python 版本了

$ pyenv install 3.7.3

去喝杯咖啡吧。 用您手烘焙的咖啡豆。 在您采摘它们之后。 我在这里说的是,这将需要一些时间。

添加虚拟环境

一旦完成,就该让您的虚拟环境使用起来更愉快了。 如果没有下一步,您实际上将为每个项目共享一个 Python 开发环境。 在每个项目的基础上使用虚拟环境来隔离依赖项管理将比 Python 开箱即用提供更多的确定性和可重现性。 出于这些原因,请将 virtualenvwrapper 安装到 Python 环境中

$ pyenv global 3.7.3
# Be sure to keep the $() syntax in this command so it can evaluate
$ $(pyenv which python3) -m pip install virtualenvwrapper

再次打开您的 .bash_profile 并添加以下内容,以确保它在每次打开新终端时都能工作

# We want to regularly go to our virtual environment directory
$ echo 'export WORKON_HOME=~/.virtualenvs' >> .bash_profile
# If in a given virtual environment, make a virtual environment directory
# If one does not already exist
$ echo 'mkdir -p $WORKON_HOME' >> .bash_profile
# Activate the new virtual environment by calling this script
# Note that $USER will substitute for your current user
$ echo '. ~/.pyenv/versions/3.7.3/bin/virtualenvwrapper.sh' >> .bash_profile

关闭终端并打开一个新的终端(或运行 exec /bin/bash -l 以刷新当前的终端会话),您将看到 virtualenvwrapper 初始化环境

$ exec /bin/bash -l
virtualenvwrapper.user_scripts creating /Users/moshe/.virtualenvs/premkproject
virtualenvwrapper.user_scripts creating /Users/moshe/.virtualenvs/postmkproject
virtualenvwrapper.user_scripts creating /Users/moshe/.virtualenvs/initialize
virtualenvwrapper.user_scripts creating /Users/moshe/.virtualenvs/premkvirtualenv
virtualenvwrapper.user_scripts creating /Users/moshe/.virtualenvs/postmkvirtualenv
virtualenvwrapper.user_scripts creating /Users/moshe/.virtualenvs/prermvirtualenv
virtualenvwrapper.user_scripts creating /Users/moshe/.virtualenvs/postrmvirtualenv
virtualenvwrapper.user_scripts creating /Users/moshe/.virtualenvs/predeactivate
virtualenvwrapper.user_scripts creating /Users/moshe/.virtualenvs/postdeactivate
virtualenvwrapper.user_scripts creating /Users/moshe/.virtualenvs/preactivate
virtualenvwrapper.user_scripts creating /Users/moshe/.virtualenvs/postactivate
virtualenvwrapper.user_scripts creating /Users/moshe/.virtualenvs/get_env_details

从现在开始,您的所有工作都应该在虚拟环境中进行,让您可以使用临时环境来安全地进行开发。 使用此工具链,您可以设置多个项目并在它们之间切换,具体取决于您当时正在处理的项目

$ mkvirtualenv test1
Using base prefix '/Users/moshe/.pyenv/versions/3.7.3'
New python executable in /Users/moshe/.virtualenvs/test1/bin/python3
Also creating executable in /Users/moshe/.virtualenvs/test1/bin/python
Installing setuptools, pip, wheel...
done.
virtualenvwrapper.user_scripts creating /Users/moshe/.virtualenvs/test1/bin/predeactivate
virtualenvwrapper.user_scripts creating /Users/moshe/.virtualenvs/test1/bin/postdeactivate
virtualenvwrapper.user_scripts creating /Users/moshe/.virtualenvs/test1/bin/preactivate
virtualenvwrapper.user_scripts creating /Users/moshe/.virtualenvs/test1/bin/postactivate
virtualenvwrapper.user_scripts creating /Users/moshe/.virtualenvs/test1/bin/get_env_details
(test1)$ mkvirtualenv test2
Using base prefix '/Users/moshe/.pyenv/versions/3.7.3'
New python executable in /Users/moshe/.virtualenvs/test2/bin/python3
Also creating executable in /Users/moshe/.virtualenvs/test2/bin/python
Installing setuptools, pip, wheel...
done.
virtualenvwrapper.user_scripts creating /Users/moshe/.virtualenvs/test2/bin/predeactivate
virtualenvwrapper.user_scripts creating /Users/moshe/.virtualenvs/test2/bin/postdeactivate
virtualenvwrapper.user_scripts creating /Users/moshe/.virtualenvs/test2/bin/preactivate
virtualenvwrapper.user_scripts creating /Users/moshe/.virtualenvs/test2/bin/postactivate
virtualenvwrapper.user_scripts creating /Users/moshe/.virtualenvs/test2/bin/get_env_details
(test2)$ ls $WORKON_HOME
get_env_details		postmkvirtualenv	premkvirtualenv
initialize		postrmvirtualenv	prermvirtualenv
postactivate		preactivate		test1
postdeactivate		predeactivate		test2
postmkproject		premkproject
(test2)$ workon test1
(test1)$

deactivate 命令使您退出当前环境。

推荐做法

您可能已经在 ~/src 等目录中设置了您的长期项目。 当您开始处理一个新项目时,进入此目录,为该项目添加一个子目录,然后使用 Bash 解释的功能根据您的目录名称命名虚拟环境。 例如,对于名为“pyfun”的项目

$ mkdir -p ~/src/pyfun && cd ~/src/pyfun
$ mkvirtualenv $(basename $(pwd))
# we will see the environment initialize
(pyfun)$ workon
pyfun
test1
test2
(pyfun)$ deactivate
$

每当您想处理此项目时,请返回该目录并通过输入以下内容重新连接到虚拟环境

$ cd ~/src/pyfun
(pyfun)$ workon .

由于初始化虚拟环境意味着获取 Python 版本和已加载模块的时间点副本,因此您偶尔需要刷新项目的虚拟环境,因为依赖项可能会发生巨大变化。 您可以通过删除虚拟环境来安全地执行此操作,因为源代码将保持完好无损

$ cd ~/src/pyfun
$ rmvirtualenv $(basename $(pwd))
$ mkvirtualenv $(basename $(pwd))

这种使用 pyenv 和 virtualwrapper 管理虚拟环境的方法将使您免于不确定您在本地开发代码时正在运行哪个 Python 版本。 这是避免混淆的最简单方法——尤其是在您与更大的团队合作时。

如果您刚开始配置 Python 环境,请阅读有关如何在 MacOS 上使用 Python 3 的文章。 您还有其他初级或中级 Python 问题吗? 请留下评论,我们将在下一篇文章中考虑它们。

接下来阅读什么
标签
Moshe sitting down, head slightly to the side. His t-shirt has Guardians of the Galaxy silhoutes against a background of sound visualization bars.
自 1998 年以来,Moshe 一直参与 Linux 社区,帮助举办 Linux “安装派对”。 自 1999 年以来,他一直在编写 Python 程序,并为核心 Python 解释器做出了贡献。 Moshe 自从 DevOps/SRE 这些术语出现之前就一直是 DevOps/SRE,他非常关心软件可靠性、构建可重现性以及其他此类事情。
I'm happiest at a microphone
Matt 曾是 EMC 存储专家、VMware vExpert 以及其他专有技术的爱好者。 他现在专注于开源和 DevRel 采用。

2 条评论

随着即将到来的 zsh 更改,提供 zsh 命令也将很有帮助。 感谢您的出色指导。

太棒了!

Creative Commons License本作品根据 Creative Commons Attribution-Share Alike 4.0 International License 许可。
© . All rights reserved.