1. Install homebrew
Official website http://brew.sh/index_zh-cn.html
Open the terminal and paste the following script in the terminal
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Test if the installation is successful
type in the terminal
brew -v
A similar prompt appears, which means the installation is successful
Homebrew 0.9.5 (git revision 1021; last commit 2016-03-30)
2. Install pyenv
type in the terminal
brew install pyenv
Verify that the installation was successful
pyenv -v
Similar results appear, which means the installation is successful
pyenv 20150310
3. View Installable Python Versions
type in the terminal
pyenv install --list
Will list installable Python version numbers
4. Install a specific version of Python
type in the terminal
pyenv install <version>
Install the corresponding Python version, such as:
pyenv install 2.7.11
5. Exception Handling
If the following exception occurs:
Installing Python-2.7.11...
ERROR: The Python zlib extension was not compiled. Missing the zlib?
Please consult to the Wiki page to fix the problem.
https://github.com/yyuu/pyenv/wiki/Common-build-problems
BUILD FAILED (OS X 10.11.5 using python-build 20160130)
Inspect or clean up the working tree at /var/folders/fb/7406jr3s60z_tdpxxqm2s9hh0000gn/T/python-build.20160616162746.48644
Results logged to /var/folders/fb/7406jr3s60z_tdpxxqm2s9hh0000gn/T/python-build.20160616162746.48644.log
Last 10 log lines:
rm -f /Users/Matrix/.pyenv/versions/2.7.11/share/man/man1/python.1
(cd /Users/Matrix/.pyenv/versions/2.7.11/share/man/man1; ln -s python2.1 python.1)
if test "xno" != "xno" ; then \
case no in \
upgrade) ensurepip="--upgrade" ;; \
install|*) ensurepip="" ;; \
esac; \
./python.exe -E -m ensurepip \
$ensurepip --root=/ ; \
fi
yujingyao:2.7.11 Matrix$ CFLAGS="-I$(brew --prefix openssl)/include" LDFLAGS="-L$(brew --prefix openssl)/lib" pyenv install 3.6-Dev
Cloning https://hg.python.org/cpython...
error: please install `mercurial` and try again
Then enter it in the terminal, pay attention to replace the version number of the following code
CFLAGS="-I$(brew --prefix openssl)/include -I$(xcrun --show-sdk-path)/usr/include" \
LDFLAGS="-L$(brew --prefix openssl)/lib" \
pyenv install -v 2.7.11
Valid in EI Capitan
Source: https://github.com/yyuu/pyenv/issues/448
For other exceptions, please refer to https://github.com/yyuu/pyenv/wiki/Common-build-problems
6. View the installed Python version of pyenv
pyenv versions
7. Edit the .bash_profile file
Enter the following command in the terminal to enter the home directory of the current user
cd ~
Enter the following command to open the .bash_profile file
open .bash_profile
If it does not exist, enter the following command to create the file
touch .bash_profile
edit file
open -e .bash_profile
.bash_profile
Add to the pop-up file
if which pyenv > /dev/null; then eval "$(pyenv init -)"; fi
command + s
save document
Enter the following command in the terminal and use the updated .bash_profile content
source .bash_profile
8. Specify the directory to switch the specified version of Python
cd to a specific directory in the terminal, modify the path name by yourself
cd /Users/Matrix/Documents/Projects/Python/3.5.1
Enter:
pyenv local <version>
9. Set the global Python version
type in the terminal
pyenv global <version>
This operation is not recommended, as some system programs may not work properly
10. Check if the switch is successful
cd to a specific directory in the terminal, modify the path name by yourself
cd /Users/Matrix/Documents/Projects/Python/3.5.1
Enter in the terminal: python
The python version used in the current directory will be listed. The same as the set version means the switch is successful.