MBP系统默认Python2.7,需要安装Python3,并且可以随时切换。
使用Homebrew安装十分简单,一条命令搞定所有,看其他小伙伴有各种报错的,但是我没有遇到。安装过程如下:
george@GeorgeMBP ~ brew install python3
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
==> New Formulae
gatsby-cli netlify-cli
==> Updated Formulae
...//期间各种依赖会自动安装好。
Python has been installed as
/usr/local/bin/python3
Unversioned symlinks `python`, `python-config`, `pip` etc. pointing to
`python3`, `python3-config`, `pip3` etc., respectively, have been installed into
/usr/local/opt/python/libexec/bin
If you need Homebrew's Python 2.7 run
brew install python@2
You can install Python packages with
pip3 install <package>
They will install into the site-package directory
/usr/local/lib/python3.7/site-packages
See: https://docs.brew.sh/Homebrew-and-Python
george@GeorgeMBP ~
配置双版本,首先在.bash_profile文件中添加如下配置:
# Setting PATH for Python 2.7
PATH="/System/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
export PATH
# Setting PATH for Python 3.7.3
PATH="/usr/local/Cellar/python/3.7.3/bin:${PATH}"
export PATH
然后在.zshrc文件中加入别名:
alias python2='/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7'
alias python3='/usr/local/Cellar/python/3.7.3/bin/python3.7'
alias python=python3
PATH的配置放在.zshrc文件中也行,不过该文件内容较多,看着比较乱,不如放在.bash_profile看着清爽。当然为了.zshrc文件能够使用.bash_profile的配置,需要在.zshrc中加入一行:
source ~/.bash_profile
配置上述配置后,运行效果如下:
george@GeorgeMBP ~ python
Python 3.7.3 (default, Jun 19 2019, 07:38:49)
[Clang 10.0.1 (clang-1001.0.46.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
george@GeorgeMBP ~ python2
Python 2.7.10 (default, Feb 22 2019, 21:55:15)
[GCC 4.2.1 Compatible Apple LLVM 10.0.1 (clang-1001.0.37.14)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
george@GeorgeMBP ~ python3
Python 3.7.3 (default, Jun 19 2019, 07:38:49)
[Clang 10.0.1 (clang-1001.0.46.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
安装配置pip,先下载安装脚本:
george@GeorgeMBP ~ curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1669k 100 1669k 0 0 400k 0 0:00:04 0:00:04 --:--:-- 400k
george@GeorgeMBP ~
使用python3安装,记得前面加上sudo否则会有权限问题失败。
george@GeorgeMBP ~ sudo python3 get-pip.py
Password:
WARNING: The directory '/Users/george/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
WARNING: The directory '/Users/george/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting pip
Downloading https://files.pythonhosted.org/packages/5c/e0/be401c003291b56efc55aeba6a80ab790d3d4cece2778288d65323009420/pip-19.1.1-py2.py3-none-any.whl (1.4MB)
|████████████████████████████████| 1.4MB 188kB/s
Installing collected packages: pip
Found existing installation: pip 19.1.1
Uninstalling pip-19.1.1:
Successfully uninstalled pip-19.1.1
Successfully installed pip-19.1.1
使用python2安装pip。
george@GeorgeMBP ~ sudo python2 get-pip.py
Password:
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7.
WARNING: The directory '/Users/george/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
WARNING: The directory '/Users/george/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting pip
Downloading https://files.pythonhosted.org/packages/5c/e0/be401c003291b56efc55aeba6a80ab790d3d4cece2778288d65323009420/pip-19.1.1-py2.py3-none-any.whl (1.4MB)
|████████████████████████████████| 1.4MB 161kB/s
Collecting wheel
Downloading https://files.pythonhosted.org/packages/bb/10/44230dd6bf3563b8f227dbf344c908d412ad2ff48066476672f3a72e174e/wheel-0.33.4-py2.py3-none-any.whl
Installing collected packages: pip, wheel
Successfully installed pip-19.1.1 wheel-0.33.4
george@GeorgeMBP ~
使用效果:
george@GeorgeMBP ~ pip2 -V
pip 19.1.1 from /Library/Python/2.7/site-packages/pip (python 2.7)
george@GeorgeMBP ~ pip -V
pip 19.1.1 from /Library/Python/2.7/site-packages/pip (python 2.7)
george@GeorgeMBP ~ pip3 -V
pip 19.1.1 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)
george@GeorgeMBP ~