使用Python的時(shí)候侮邀,有些應(yīng)用軟件用Python2念链,有的用Python3盼忌,因此需要自己的系統(tǒng)在多個(gè)版本之間切換。那如何方便地安裝管理多個(gè)版本Python呢掂墓?小白這里用到了Homebrew和pyenv工具谦纱。
Homebrew是一款MacOS平臺下的軟件包管理工具,可以實(shí)現(xiàn)包管理君编,而不用你關(guān)心各種依賴和文件路徑的情況跨嘉,十分方便快捷。
不容易理解吧吃嘿,簡單來說祠乃,就是手機(jī)里的AppStore。懂了吧兑燥。
pyenv是管理python版本的工具,管理各種python版本降瞳,并且各個(gè)版本的環(huán)境完全獨(dú)立嘱支,互不干擾蚓胸。
嘉賓介紹完了就開始吧。??
1. 首先安裝Homebrew除师。網(wǎng)上有很多舊版本的沛膳,還是用ruby安裝的,但是終端自己會提示已經(jīng)被移除了汛聚。所以直接用新的就可以啦锹安。
/bin/bash -c "$(curl -fsSL [https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/install.sh](https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/install.sh))"
遇到問題說沒有shallow啥的,不懂倚舀,不過還好給出了建議代碼八毯。照著運(yùn)行就好了。
git -C /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core fetch –unshallow
或許還有其他的問題瞄桨,請參考Homebrew
運(yùn)行一下话速,安裝成功啦!o( ̄▽ ̄)o
brew -v
#Homebrew 3.2.2
#Homebrew/homebrew-core (git revision 015c9ed8c5; last commit 2021-07-13)
2. 然后安裝pyenv
brew update
#Already up-to-date.
brew install pyenv
#這里有warning了
#==> Installing python@2
#Error: Could not symlink #Frameworks/Python.framework/Headers
#Target /usr/local/Frameworks/Python.framework/Headers
#is a symlink belonging to python@3.9\. You can unlink it:
# brew unlink python@3.9
#To force the link and overwrite all conflicting files:
#brew link --overwrite python@2
#To list all files that would be deleted:
# brew link --overwrite --dry-run python@2
pyenv -v
#pyenv 2.0.3
雖然pyenv也裝上了芯侥,但是這個(gè)警告會影響后續(xù)python的安裝泊交,這里可能是因?yàn)槲业碾娔X本來就裝了python2導(dǎo)致的。其實(shí)按照提示代碼運(yùn)行就好了柱查。
brew unlink [python@3.9](mailto:python@3.9)
brew link --overwrite python@2
3. 然后就可以安裝python2了
pyenv install 2.7.15
然而安裝python3的時(shí)候出了問題??
pyenv install 3.6.13
#報(bào)錯(cuò):
#1 error generated.
#make: *** [Modules/posixmodule.o] Error 1
#make: *** Waiting for unfinished jobs....
經(jīng)過搜索廓俭,兩個(gè)原因,一是xcode的問題唉工,要安裝zlib bzip2研乒,二是版本也有問題,換成3.6.13淋硝。就成功了雹熬。??
brew reinstall zlib bzip2
CFLAGS="-I$(brew --prefix openssl)/include -I$(brew --prefix bzip2)/include -I$(brew --prefix readline)/include -I$(xcrun --show-sdk-path)/usr/include" LDFLAGS="-L$(brew --prefix openssl)/lib -L$(brew --prefix readline)/lib -L$(brew --prefix zlib)/lib -L$(brew --prefix bzip2)/lib" pyenv install --patch 3.6.13 < <(curl -sSL https://github.com/python/cpython/commit/8ea6353.patch\?full_index\=1)
請注意要改你需要安裝的Python版本號。
如果沒有成功谣膳,參考unable to install python 3.8.0 on macox 11
查看兩個(gè)版本已經(jīng)安裝竿报。*號是當(dāng)前的版本,system是系統(tǒng)自帶继谚。
pyenv versions
#* system (set by ~/.pyenv/version)
# 2.7.15
# 3.6.13
切換版本烈菌,看到*去了定義的Python3。
pyenv global 3.6.13
pyenv versions
# system
# 2.7.15
#* 3.6.13 (set by /Users/liuyuhao/.pyenv/version)
4. 然而又進(jìn)坑里去了花履,終端輸入python命令還是系統(tǒng)自帶的python2
python
#Python 2.7.16 (default, Dec 21 2020, 23:00:36)
是不是當(dāng)前shell語言沒設(shè)置芽世?那把當(dāng)前shell的python語言設(shè)置成python3.6.13
pyenv shell 3.6.13
#報(bào)錯(cuò)
#pyenv: shell integration not enabled. Run `pyenv init' for instructions.
那就按照建議代碼運(yùn)行
pyenv init
# (The below instructions are intended for common
# shell setups. See the README for more guidance
# if they don't apply and/or don't work for you.)
# Add pyenv executable to PATH and
1 export PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
# enable shims by adding the following
# to ~/.profile:
1 export PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init --path)"
# If your ~/.profile sources ~/.bashrc,
# the lines need to be inserted before the part
1 export PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
# that does that. See the README for another option.
# If you have ~/.bash_profile, make sure that it
# also executes the above lines -- e.g. by
# copying them there or by sourcing ~/.profile
# Load pyenv into the shell by adding
# the following to ~/.bashrc:
eval "$(pyenv init -)"
# Make sure to restart your entire logon session
# for changes to profile files to take effect.
按照它的要求把下面的語句手動(dòng)寫入~/.bash_profile
export PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init --path)"
eval "$(pyenv init -)"
寫完別忘了更新
source ~/.bash_profile
再運(yùn)行就成功啦! o( ̄▽ ̄)
python
#Python 3.6.13 (default, Jul 14 2021, 14:31:09)