Mac 的安装方式和两个版本共存的出现的问题:
Python 安装包下载地址:https://www.python.org/downloads/
如果要Python 2x 和 Python 3x 同时拥有的话,我是这样做的:
1. 分别安装两个安装包。
2. 安装完成之后配置环境使得能在终端切换Python版本:
2.1 打开终端(terminal),输入:sudo vi ~/.bashrc;
2.2 在弹出的编辑页面顶部输入并保存:
alias python2='/Library/Frameworks/Python.framework/Versions/2.x/bin/python2.x'
alias python3='/Library/Frameworks/Python.framework/Versions/3.x/bin/python3.x'
我机子上有Python2.7和Python3.6版本,上面的x请根据情况自行替换。
2.3 重启终端(terminal)或者输入:source ~/.bashrc
2.4 验证,在终端(terminal)中输入python2即代表是Python2.x版本,输入python3即代表是Python3.x版本。
3. 完成上面的步骤后,应该来说没问题了。接下来就是通过pip 安装 第三方模块。
在这里我说一下我遇到的情况,我是先安装Python 3.6.3, 再安装Python 2.7.14, 在pip3 install xx 是通过的,在在pip2 install xx 就报错了:
$ pip2 install requests
Collecting requests
Could not fetch URL https://pypi.python.org/simple/requests/: There was a problem confirming the ssl certificate: [SSL: TLSV1_ALERT_PROTOCOL_VERSION] tlsv1 alert protocol version (_ssl.c:661) - skipping
Could not find a version that satisfies the requirement requests (from versions: )
No matching distribution found for requests
You are using pip version 9.0.1, however version 10.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
我按照提示更新pip install --upgrade pip
,咦~没用:
$ pip install --upgrade pip
Could not fetch URL https://pypi.python.org/simple/pip/: There was a problem confirming the ssl certificate: [SSL: TLSV1_ALERT_PROTOCOL_VERSION] tlsv1 alert protocol version (_ssl.c:661) - skipping
Requirement already up-to-date: pip in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
You are using pip version 9.0.1, however version 10.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
后面想想,我pip 都无效,应该选择有效的pip3 进行更新:
$ pip3 install --upgrade pip
Collecting pip
Downloading https://files.pythonhosted.org/packages/0f/74/ecd13431bcc456ed390b44c8a6e917c1820365cbebcb6a8974d1cd045ab4/pip-10.0.1-py2.py3-none-any.whl (1.3MB)
100% |████████████████████████████████| 1.3MB 623kB/s
Installing collected packages: pip
Found existing installation: pip 9.0.3
Uninstalling pip-9.0.3:
Successfully uninstalled pip-9.0.3
Successfully installed pip-10.0.1
cfzxdeiMac:~ cfzx$ pip2 install --upgrade pip
Collecting pip
Using cached https://files.pythonhosted.org/packages/0f/74/ecd13431bcc456ed390b44c8a6e917c1820365cbebcb6a8974d1cd045ab4/pip-10.0.1-py2.py3-none-any.whl
Installing collected packages: pip
Found existing installation: pip 9.0.1
Uninstalling pip-9.0.1:
Successfully uninstalled pip-9.0.1
Successfully installed pip-10.0.1
成功之后我再试试看:
$ pip2 install requests
Collecting requests
……
Successfully installed certifi-2018.4.16 chardet-3.0.4 idna-2.6 requests-2.18.4 urllib3-1.22