MacOS系统中如何设置Python虚拟环境

系统 MacOS 后端
作为 Python 开发者和 MacOS 用户,拿到新机器首先要做的就是设置 Python 开发环境。

[[270394]]

使用 pyenv 和 virtualwrapper 来管理你的虚拟环境,可以避免很多困惑。

作为 Python 开发者和 MacOS 用户,拿到新机器首先要做的就是设置 Python 开发环境。下面是***实践(虽然我们已经写过 在 MacOS 上管理 Python 的其它方法)。

预备

首先,打开终端,在其冰冷毫无提示的窗口输入 xcode-select --install 命令。点击确认后,基本的开发环境就会被配置上。MacOS 上需要此步骤来设置本地开发实用工具库,根据 OS X Daily 的说法,其包括 ”许多常用的工具、实用程序和编译器,如 make、GCC、clang、perl、svn、git、size、strip、strings、libtool、cpp、what 及许多在 Linux 中系统默认安装的有用命令“。

接下来,安装 Homebrew, 执行如下的 Ruby 脚本。

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

如果你像我一样,对随意就运行的来源于互联网的脚本心存疑虑的话,可以点击上面的脚本去仔细看看其具体功能。

一旦安装完成后,就恭喜了,你拥有了一个优秀的包管理工具。自然的,你可能接下来会执行 brew install python 或其他的命令。不要这样,哈哈!Homebrew 是为我们提供了一个 Python 的管理版本,但让此工具来管理我们的 Python 环境话,很快会失控的。我们需要 pyenv,一款简单的 Python 版本管理工具,它可以安装运行在 许多操作系统 上。运行如下命令:

  1. $ brew install pyenv

想要每次打开命令提示框时 pyenv 都会运行的话,需要把下面的内容加入你的配置文件中(MacOS 中默认为 .bash_profile,位于家目录下):

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

添加此行内容后,每个终端都会启动 pyenv 来管理其 PATH 环境变量,并插入你想要运行的 Python 版本(而不是在环境变量里面设置的初始版本。更详细的信息,请阅读 “如何给 Linux 系统设置 PATH 变量”)。打开新的终端以使修改的 .bash_profile 文件生效。

在安装你中意的 Python 版本前,需要先安装一些有用的工具,如下示:

  1. $ brew install zlib sqlite

pyenv 依赖于 zlib 压缩算法和 SQLite 数据库,如果未正确配置,往往会导致构建问题。将这些导出配置命令加入当前的终端窗口执行,确保它们安装完成。

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

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

  1. $ pyenv install 3.7.3

去喝杯咖啡吧,挑些豆类,亲自烧烤,然后品尝。说这些的意思是上面的安装过程需要一段时间。

添加虚拟环境

一旦完成,就可以愉快地使用虚拟环境了。如没有接下来的步骤的话,你只能在你所有的工作项目中共享同一个 Python 开发环境。使用虚拟环境来隔离每个项目的依赖关系的管理方式,比起 Python 自身提供的开箱即用功能来说,更加清晰明确和更具有重用性。基于这些原因,把 virtualenvwrapper 安装到 Python 环境中吧:

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

再次打开 .bash_profile 文件,把下面内容添加进去,使得每次打开新终端时它都有效:

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

关掉终端再重新打开(或者运行 exec /bin/bash -l 来刷新当前的终端会话),你会看到 virtualenvwrapper 正在初始化环境配置:

  1. $ exec /bin/bash -l
  2. virtualenvwrapper.user_scripts creating /Users/moshe/.virtualenvs/premkproject
  3. virtualenvwrapper.user_scripts creating /Users/moshe/.virtualenvs/postmkproject
  4. virtualenvwrapper.user_scripts creating /Users/moshe/.virtualenvs/initialize
  5. virtualenvwrapper.user_scripts creating /Users/moshe/.virtualenvs/premkvirtualenv
  6. virtualenvwrapper.user_scripts creating /Users/moshe/.virtualenvs/postmkvirtualenv
  7. virtualenvwrapper.user_scripts creating /Users/moshe/.virtualenvs/prermvirtualenv
  8. virtualenvwrapper.user_scripts creating /Users/moshe/.virtualenvs/postrmvirtualenv
  9. virtualenvwrapper.user_scripts creating /Users/moshe/.virtualenvs/predeactivate
  10. virtualenvwrapper.user_scripts creating /Users/moshe/.virtualenvs/postdeactivate
  11. virtualenvwrapper.user_scripts creating /Users/moshe/.virtualenvs/preactivate
  12. virtualenvwrapper.user_scripts creating /Users/moshe/.virtualenvs/postactivate
  13. virtualenvwrapper.user_scripts creating /Users/moshe/.virtualenvs/get_env_details

从此刻开始,你的所有工作都是在虚拟环境中的,其允许你使用临时环境来安全地开发。使用此工具链,你可以根据工作所需,设置多个项目并在它们之间切换:

  1. $ mkvirtualenv test1
  2. Using base prefix '/Users/moshe/.pyenv/versions/3.7.3'
  3. New python executable in /Users/moshe/.virtualenvs/test1/bin/python3
  4. Also creating executable in /Users/moshe/.virtualenvs/test1/bin/python
  5. Installing setuptools, pip, wheel...
  6. done.
  7. virtualenvwrapper.user_scripts creating /Users/moshe/.virtualenvs/test1/bin/predeactivate
  8. virtualenvwrapper.user_scripts creating /Users/moshe/.virtualenvs/test1/bin/postdeactivate
  9. virtualenvwrapper.user_scripts creating /Users/moshe/.virtualenvs/test1/bin/preactivate
  10. virtualenvwrapper.user_scripts creating /Users/moshe/.virtualenvs/test1/bin/postactivate
  11. virtualenvwrapper.user_scripts creating /Users/moshe/.virtualenvs/test1/bin/get_env_details
  12. (test1)$ mkvirtualenv test2
  13. Using base prefix '/Users/moshe/.pyenv/versions/3.7.3'
  14. New python executable in /Users/moshe/.virtualenvs/test2/bin/python3
  15. Also creating executable in /Users/moshe/.virtualenvs/test2/bin/python
  16. Installing setuptools, pip, wheel...
  17. done.
  18. virtualenvwrapper.user_scripts creating /Users/moshe/.virtualenvs/test2/bin/predeactivate
  19. virtualenvwrapper.user_scripts creating /Users/moshe/.virtualenvs/test2/bin/postdeactivate
  20. virtualenvwrapper.user_scripts creating /Users/moshe/.virtualenvs/test2/bin/preactivate
  21. virtualenvwrapper.user_scripts creating /Users/moshe/.virtualenvs/test2/bin/postactivate
  22. virtualenvwrapper.user_scripts creating /Users/moshe/.virtualenvs/test2/bin/get_env_details
  23. (test2)$ ls $WORKON_HOME
  24. get_env_details postmkvirtualenv premkvirtualenv
  25. initialize postrmvirtualenv prermvirtualenv
  26. postactivate preactivate test1
  27. postdeactivate predeactivate test2
  28. postmkproject premkproject
  29. (test2)$ workon test1
  30. (test1)$

此处,使用 deactivate 命令可以退出当前环境。

推荐实践

你可能已经在比如 ~/src 这样的目录中添加了长期的项目。当要开始了一个新项目时,进入此目录,为此项目增加子文件夹,然后使用强大的 Bash 解释程序自动根据你的目录名来命令虚拟环境。例如,名称为 “pyfun” 的项目:

  1. $ mkdir -p ~/src/pyfun && cd ~/src/pyfun
  2. $ mkvirtualenv $(basename $(pwd))
  3. # we will see the environment initialize
  4. (pyfun)$ workon
  5. pyfun
  6. test1
  7. test2
  8. (pyfun)$ deactivate
  9. $

当需要处理此项目时,只要进入该目录,输入如下命令重新连接虚拟环境:

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

初始化虚拟环境意味着对 Python 版本和所加载的模块的时间点的拷贝。由于依赖关系会发生很大的改变,所以偶尔需要刷新项目的虚拟环境。这种情况,你可以通过删除虚拟环境来安全的执行此操作,源代码是不受影响的,如下所示:

  1. $ cd ~/src/pyfun
  2. $ rmvirtualenv $(basename $(pwd))
  3. $ mkvirtualenv $(basename $(pwd))

这种使用 pyenvvirtualwrapper 管理虚拟环境的方法可以避免开发环境和运行环境中 Python 版本的不一致出现的苦恼。这是避免混淆的最简单方法 - 尤其是你工作的团队很大的时候。

如果你是初学者,正准备配置 Python 环境,可以阅读下 MacOS 中使用 Python 3 文章。 你们有关于 Python 相关的问题吗,不管是初学者的还是中级使用者的?给我们留下评论信息,我们在下篇文章中会考虑讲解。

责任编辑:庞桂玉 来源: Linux中国
相关推荐

2021-10-28 19:08:29

Python虚拟环境

2023-06-01 15:37:11

PyCharm工具开发

2021-07-30 20:25:04

pipxPython编程语言

2013-07-15 10:18:07

2013-07-11 09:11:16

2012-11-30 16:02:57

在vSphereiSCSI存储虚拟化

2023-05-05 16:37:13

CPU 性能虚拟环境虚拟化

2021-09-18 08:00:00

Python系统开发

2021-03-04 20:46:32

virtualenvwPython虚拟环境

2021-11-15 05:44:16

Python虚拟环境开发

2017-01-06 09:57:08

AppSenseLANDesk桌面

2021-08-13 08:42:48

Python 编程技巧poetry

2012-09-20 10:35:40

VMware虚拟环境公有云

2020-07-06 07:00:21

PyCharm虚拟环境

2020-05-22 08:52:08

LinuxPython工具

2017-06-27 05:08:46

存储虚拟化服务器

2019-07-02 06:31:27

Python虚拟环境代码

2013-10-11 16:47:38

2021-07-05 06:51:42

Python虚拟环境

2021-08-13 07:56:13

Python虚拟环境
点赞
收藏

51CTO技术栈公众号