MacOs安裝多個版本的Python

使用Python的時候,有些應(yīng)用軟件用Python2,有的用Python3,因此需要自己的系統(tǒng)在多個版本之間切換。那如何方便地安裝管理多個版本Python呢?小白這里用到了Homebrewpyenv工具。

Homebrew介紹和使用

Homebrew是一款MacOS平臺下的軟件包管理工具,可以實現(xiàn)包管理,而不用你關(guān)心各種依賴和文件路徑的情況,十分方便快捷。

不容易理解吧,簡單來說,就是手機里的AppStore。懂了吧。

pyenv,讓你輕松切換各種python版本

pyenv是管理python版本的工具,管理各種python版本,并且各個版本的環(huán)境完全獨立,互不干擾。

嘉賓介紹完了就開始吧。??

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啥的,不懂,不過還好給出了建議代碼。照著運行就好了。

git -C /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core fetch –unshallow

或許還有其他的問題,請參考Homebrew

運行一下,安裝成功啦!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也裝上了,但是這個警告會影響后續(xù)python的安裝,這里可能是因為我的電腦本來就裝了python2導(dǎo)致的。其實按照提示代碼運行就好了。

brew unlink [python@3.9](mailto:python@3.9)
brew link --overwrite python@2

3. 然后就可以安裝python2了

pyenv install 2.7.15

然而安裝python3的時候出了問題??

pyenv install 3.6.13
#報錯:
#1 error generated.
#make: *** [Modules/posixmodule.o] Error 1
#make: *** Waiting for unfinished jobs....

經(jīng)過搜索,兩個原因,一是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

查看兩個版本已經(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. 然而又進坑里去了,終端輸入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
#報錯
#pyenv: shell integration not enabled. Run `pyenv init' for instructions.

那就按照建議代碼運行

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.

按照它的要求把下面的語句手動寫入~/.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

再運行就成功啦! o( ̄▽ ̄)

python
#Python 3.6.13 (default, Jul 14 2021, 14:31:09)

參考:Python: 配置pyenv virtualenv 環(huán)境

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容