MacOS系統(tǒng)中如何設置Python虛擬環(huán)境
使用 pyenv 和 virtualwrapper 來管理你的虛擬環(huán)境,可以避免很多困惑。
作為 Python 開發(fā)者和 MacOS 用戶,拿到新機器首先要做的就是設置 Python 開發(fā)環(huán)境。下面是***實踐(雖然我們已經(jīng)寫過 在 MacOS 上管理 Python 的其它方法)。
預備
首先,打開終端,在其冰冷毫無提示的窗口輸入 xcode-select --install
命令。點擊確認后,基本的開發(fā)環(huán)境就會被配置上。MacOS 上需要此步驟來設置本地開發(fā)實用工具庫,根據(jù) OS X Daily 的說法,其包括 ”許多常用的工具、實用程序和編譯器,如 make、GCC、clang、perl、svn、git、size、strip、strings、libtool、cpp、what 及許多在 Linux 中系統(tǒng)默認安裝的有用命令“。
接下來,安裝 Homebrew, 執(zhí)行如下的 Ruby 腳本。
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
如果你像我一樣,對隨意就運行的來源于互聯(lián)網(wǎng)的腳本心存疑慮的話,可以點擊上面的腳本去仔細看看其具體功能。
一旦安裝完成后,就恭喜了,你擁有了一個優(yōu)秀的包管理工具。自然的,你可能接下來會執(zhí)行 brew install python
或其他的命令。不要這樣,哈哈!Homebrew 是為我們提供了一個 Python 的管理版本,但讓此工具來管理我們的 Python 環(huán)境話,很快會失控的。我們需要 pyenv,一款簡單的 Python 版本管理工具,它可以安裝運行在 許多操作系統(tǒng) 上。運行如下命令:
$ brew install pyenv
想要每次打開命令提示框時 pyenv
都會運行的話,需要把下面的內容加入你的配置文件中(MacOS 中默認為 .bash_profile
,位于家目錄下):
$ cd ~/
$ echo 'eval "$(pyenv init -)"' >> .bash_profile
添加此行內容后,每個終端都會啟動 pyenv
來管理其 PATH
環(huán)境變量,并插入你想要運行的 Python 版本(而不是在環(huán)境變量里面設置的初始版本。更詳細的信息,請閱讀 “如何給 Linux 系統(tǒng)設置 PATH 變量”)。打開新的終端以使修改的 .bash_profile
文件生效。
在安裝你中意的 Python 版本前,需要先安裝一些有用的工具,如下示:
$ brew install zlib sqlite
pyenv
依賴于 zlib 壓縮算法和 SQLite 數(shù)據(jù)庫,如果未正確配置,往往會導致構建問題。將這些導出配置命令加入當前的終端窗口執(zhí)行,確保它們安裝完成。
$ 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"
現(xiàn)在準備工作已經(jīng)完成,是時候安裝一個適合于現(xiàn)代人的 Python 版本了:
$ pyenv install 3.7.3
去喝杯咖啡吧,挑些豆類,親自燒烤,然后品嘗。說這些的意思是上面的安裝過程需要一段時間。
添加虛擬環(huán)境
一旦完成,就可以愉快地使用虛擬環(huán)境了。如沒有接下來的步驟的話,你只能在你所有的工作項目中共享同一個 Python 開發(fā)環(huán)境。使用虛擬環(huán)境來隔離每個項目的依賴關系的管理方式,比起 Python 自身提供的開箱即用功能來說,更加清晰明確和更具有重用性。基于這些原因,把 virtualenvwrapper
安裝到 Python 環(huán)境中吧:
$ 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
正在初始化環(huán)境配置:
$ 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
從此刻開始,你的所有工作都是在虛擬環(huán)境中的,其允許你使用臨時環(huán)境來安全地開發(fā)。使用此工具鏈,你可以根據(jù)工作所需,設置多個項目并在它們之間切換:
$ 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
命令可以退出當前環(huán)境。
推薦實踐
你可能已經(jīng)在比如 ~/src
這樣的目錄中添加了長期的項目。當要開始了一個新項目時,進入此目錄,為此項目增加子文件夾,然后使用強大的 Bash 解釋程序自動根據(jù)你的目錄名來命令虛擬環(huán)境。例如,名稱為 “pyfun” 的項目:
$ mkdir -p ~/src/pyfun && cd ~/src/pyfun
$ mkvirtualenv $(basename $(pwd))
# we will see the environment initialize
(pyfun)$ workon
pyfun
test1
test2
(pyfun)$ deactivate
$
當需要處理此項目時,只要進入該目錄,輸入如下命令重新連接虛擬環(huán)境:
$ cd ~/src/pyfun
(pyfun)$ workon .
初始化虛擬環(huán)境意味著對 Python 版本和所加載的模塊的時間點的拷貝。由于依賴關系會發(fā)生很大的改變,所以偶爾需要刷新項目的虛擬環(huán)境。這種情況,你可以通過刪除虛擬環(huán)境來安全的執(zhí)行此操作,源代碼是不受影響的,如下所示:
$ cd ~/src/pyfun
$ rmvirtualenv $(basename $(pwd))
$ mkvirtualenv $(basename $(pwd))
這種使用 pyenv
和 virtualwrapper
管理虛擬環(huán)境的方法可以避免開發(fā)環(huán)境和運行環(huán)境中 Python 版本的不一致出現(xiàn)的苦惱。這是避免混淆的最簡單方法 - 尤其是你工作的團隊很大的時候。
如果你是初學者,正準備配置 Python 環(huán)境,可以閱讀下 MacOS 中使用 Python 3 文章。 你們有關于 Python 相關的問題嗎,不管是初學者的還是中級使用者的?給我們留下評論信息,我們在下篇文章中會考慮講解。