使用Python的时候,有些应用软件用Python2,有的用Python3,因此需要自己的系统在多个版本之间切换。那如何方便地安装管理多个版本Python呢?小白这里用到了Homebrew和pyenv工具。
Homebrew是一款MacOS平台下的软件包管理工具,可以实现包管理,而不用你关心各种依赖和文件路径的情况,十分方便快捷。
不容易理解吧,简单来说,就是手机里的AppStore。懂了吧。
pyenv是管理python版本的工具,管理各种python版本,并且各个版本的环境完全独立,互不干扰。
嘉宾介绍完了就开始吧。⛽️
1. 首先安装Homebrew。网上有很多旧版本的,还是用ruby安装的,但是终端自己会提示已经被移除了。所以直接用新的就可以啦。
/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也装上了,但是这个警告会影响后续python的安装,这里可能是因为我的电脑本来就装了python2导致的。其实按照提示代码运行就好了。
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....
经过搜索,两个原因,一是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
查看两个版本已经安装。*号是当前的版本,system是系统自带。
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命令还是系统自带的python2
python
#Python 2.7.16 (default, Dec 21 2020, 23:00:36)
是不是当前shell语言没设置?那把当前shell的python语言设置成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)