When using Python, some applications use Python2 and some use Python3, so you need your own system to switch between multiple versions. How to easily install and manage multiple versions of Python? Xiaobai used Homebrew and pyenv tools here.
Homebrew is a package management tool under the MacOS platform, which can implement package management without you having to care about various dependencies and file paths, which is very convenient and fast.
It's not easy to understand. Simply put, it is the AppStore in the mobile phone. got it.
pyenv is a tool for managing python versions, managing various python versions, and the environment of each version is completely independent and does not interfere with each other.
There are many old versions on the Internet, which are still installed with ruby, but the terminal itself will prompt that it has been removed. So just use the new one.
/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))"
Encountered a problem and said that there is no shallow, I don't understand, but fortunately, I gave a suggestion code. Just run it.
git -C /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core fetch –unshallow
Maybe there are other questions, please refer to Homebrew
Run it, the installation is successful! o(￣▽￣)o
brew -v #Homebrew 3.2.2 #Homebrew/homebrew-core (git revision 015c9ed8c5; last commit 2021-07-13)
brew update #Already up-to-date.
brew install pyenv #There is a warning here #==> Installing python@2 #Error: Could not symlink #Frameworks/Python.framework/Headers #Target /usr/local/Frameworks/Python.framework/Headers #is a symlink belonging to firstname.lastname@example.org\. You can unlink it: # brew unlink email@example.com #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
Although pyenv is also installed, this warning will affect the subsequent installation of python. This may be because my computer is already installed with python2. In fact, just follow the prompt code to run it.
brew unlink [firstname.lastname@example.org](mailto:email@example.com) brew link --overwrite python@2
pyenv install 2.7.15
However, something went wrong when installing python3
pyenv install 3.6.13 #error： #1 error generated. #make: *** [Modules/posixmodule.o] Error 1 #make: *** Waiting for unfinished jobs....
After searching, there are two reasons, one is a problem with xcode, zlib bzip2 needs to be installed, and the other is a problem with the version, which is changed to 3.6.13. It succeeded.
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)
Please note that you need to change the Python version number you need to install.
Check that the two versions have been installed. The
* number is the current version, and system is the system's own.
pyenv versions #* system (set by ~/.pyenv/version) # 2.7.15 # 3.6.13
Switch version, see * go to the definition of Python3.
pyenv global 3.6.13 pyenv versions # system # 2.7.15 #* 3.6.13 (set by /Users/liuyuhao/.pyenv/version)
4. However, I went into the pit again, the python command input in the terminal is still the python2 that comes with the system
python #Python 2.7.16 (default, Dec 21 2020, 23:00:36)
Is the current shell language not set? Then set the python language of the current shell to python3.6.13
pyenv shell 3.6.13 #error #pyenv: shell integration not enabled. Run `pyenv init' for instructions.
Then follow the suggested code to run
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.
According to its requirements, manually write the following statements into
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 -)"
Don't forget to update after writing
Run again to succeed! o(￣▽￣)
python #Python 3.6.13 (default, Jul 14 2021, 14:31:09)