- 資訊首頁(yè) > 網(wǎng)絡(luò )安全 >
- MacOS系統中如何設置Python虛擬環(huán)境
使用 pyenv 和 virtualwrapper 來(lái)管理你的虛擬環(huán)境,可以避免很多困惑。
作為 Python 開(kāi)發(fā)者和 MacOS 用戶(hù),拿到新機器首先要做的就是設置 Python 開(kāi)發(fā)環(huán)境。下面是最佳實(shí)踐(雖然我們已經(jīng)寫(xiě)過(guò) 在 MacOS 上管理 Python 的其它方法)。
預備
首先,打開(kāi)終端,在其冰冷毫無(wú)提示的窗口輸入 xcode-select --install 命令。點(diǎn)擊確認后,基本的開(kāi)發(fā)環(huán)境就會(huì )被配置上。MacOS 上需要此步驟來(lái)設置本地開(kāi)發(fā)實(shí)用工具庫,根據 OS X Daily 的說(shuō)法,其包括 ”許多常用的工具、實(shí)用程序和編譯器,如 make、GCC、clang、perl、svn、git、size、strip、strings、libtool、cpp、what 及許多在 Linux 中系統默認安裝的有用命令“。
接下來(lái),安裝 Homebrew, 執行如下的 Ruby 腳本。
ruby -e "$(curl -f http://wap.friendlycc.com.cn/ 如果你像我一樣,對隨意就運行的來(lái)源于互聯(lián)網(wǎng)的腳本心存疑慮的話(huà),可以點(diǎn)擊上面的腳本去仔細看看其具體功能。
一旦安裝完成后,就恭喜了,你擁有了一個(gè)優(yōu)秀的包管理工具。自然的,你可能接下來(lái)會(huì )執行 brew install python 或其他的命令。不要這樣,哈哈!Homebrew 是為我們提供了一個(gè) Python 的管理版本,但讓此工具來(lái)管理我們的 Python 環(huán)境話(huà),很快會(huì )失控的。我們需要 pyenv,一款簡(jiǎn)單的 Python 版本管理工具,它可以安裝運行在 許多操作系統 上。運行如下命令:
$ brew install pyenv
想要每次打開(kāi)命令提示框時(shí) pyenv 都會(huì )運行的話(huà),需要把下面的內容加入你的配置文件中(MacOS 中默認為 .bash_profile,位于家目錄下):
$ cd ~/
$ echo 'eval "$(pyenv init -)"' >> .bash_profile
添加此行內容后,每個(gè)終端都會(huì )啟動(dòng) pyenv 來(lái)管理其 PATH 環(huán)境變量,并插入你想要運行的 Python 版本(而不是在環(huán)境變量里面設置的初始版本。更詳細的信息,請閱讀 “如何給 Linux 系統設置 PATH 變量”)。打開(kāi)新的終端以使修改的 .bash_profile 文件生效。
在安裝你中意的 Python 版本前,需要先安裝一些有用的工具,如下示:
$ brew install zlib sqlite
pyenv 依賴(lài)于 zlib 壓縮算法和 SQLite 數據庫,如果未正確配置,往往會(huì )導致構建問(wèn)題。將這些導出配置命令加入當前的終端窗口執行,確保它們安裝完成。
$ 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"
現在準備工作已經(jīng)完成,是時(shí)候安裝一個(gè)適合于現代人的 Python 版本了:
$ pyenv install 3.7.3
去喝杯咖啡吧,挑些豆類(lèi),親自燒烤,然后品嘗。說(shuō)這些的意思是上面的安裝過(guò)程需要一段時(shí)間。
添加虛擬環(huán)境
一旦完成,就可以愉快地使用虛擬環(huán)境了。如沒(méi)有接下來(lái)的步驟的話(huà),你只能在你所有的工作項目中共享同一個(gè) Python 開(kāi)發(fā)環(huán)境。使用虛擬環(huán)境來(lái)隔離每個(gè)項目的依賴(lài)關(guān)系的管理方式,比起 Python 自身提供的開(kāi)箱即用功能來(lái)說(shuō),更加清晰明確和更具有重用性?;谶@些原因,把 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
再次打開(kāi) .bash_profile 文件,把下面內容添加進(jìn)去,使得每次打開(kāi)新終端時(shí)它都有效:
# 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
關(guān)掉終端再重新打開(kāi)(或者運行 exec /bin/bash -l 來(lái)刷新當前的終端會(huì )話(huà)),你會(huì )看到 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
從此刻開(kāi)始,你的所有工作都是在虛擬環(huán)境中的,其允許你使用臨時(shí)環(huán)境來(lái)安全地開(kāi)發(fā)。使用此工具鏈,你可以根據工作所需,設置多個(gè)項目并在它們之間切換:
$ 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)境。
推薦實(shí)踐
你可能已經(jīng)在比如 ~/src 這樣的目錄中添加了長(cháng)期的項目。當要開(kāi)始了一個(gè)新項目時(shí),進(jìn)入此目錄,為此項目增加子文件夾,然后使用強大的 Bash 解釋程序自動(dòng)根據你的目錄名來(lái)命令虛擬環(huán)境。例如,名稱(chēng)為 “pyfun” 的項目:
$ mkdir -p ~/src/pyfun && cd ~/src/pyfun
$ mkvirtualenv $(basename $(pwd))
# we will see the environment initialize
(pyfun)$ workon
pyfun
test1
test2
(pyfun)$ deactivate
$
當需要處理此項目時(shí),只要進(jìn)入該目錄,輸入如下命令重新連接虛擬環(huán)境:
$ cd ~/src/pyfun
(pyfun)$ workon .
初始化虛擬環(huán)境意味著(zhù)對 Python 版本和所加載的模塊的時(shí)間點(diǎn)的拷貝。由于依賴(lài)關(guān)系會(huì )發(fā)生很大的改變,所以偶爾需要刷新項目的虛擬環(huán)境。這種情況,你可以通過(guò)刪除虛擬環(huán)境來(lái)安全的執行此操作,源代碼是不受影響的,如下所示:
$ cd ~/src/pyfun
$ rmvirtualenv $(basename $(pwd))
$ mkvirtualenv $(basename $(pwd))
這種使用 pyenv 和 virtualwrapper 管理虛擬環(huán)境的方法可以避免開(kāi)發(fā)環(huán)境和運行環(huán)境中 Python 版本的不一致出現的苦惱。這是避免混淆的最簡(jiǎn)單方法 - 尤其是你工作的團隊很大的時(shí)候。
如果你是初學(xué)者,正準備配置 Python 環(huán)境,可以閱讀下 MacOS 中使用 Python 3 文章。 你們有關(guān)于 Python 相關(guān)的問(wèn)題嗎,不管是初學(xué)者的還是中級使用者的?給我們留下評論信息,我們在下篇文章中會(huì )考慮講解。
【上云季-1折驚爆價(jià)】香港/美國高性能云服務(wù)器27元起秒殺專(zhuān)場(chǎng),上云必備,火爆開(kāi)搶>>點(diǎn)擊查看詳情<<
【全球云-高性能限量搶】高性能云主機好用能省,高性能高配置高儲存,輕松解鎖云端場(chǎng)景>>點(diǎn)擊查看詳情<<
【服務(wù)器-特價(jià)服務(wù)器】新購指定配置可享受限時(shí)特惠7折。每個(gè)用戶(hù)不限臺數 低至280元月>>點(diǎn)擊查看詳情<<
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系QQ:712375056。
Copyright ? 2009-2021 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 珠海市特網(wǎng)科技有限公司 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站