概述
Mac电脑中默认安装的是Python2.7,而有时会提示
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support pip 21.0 will remove support for this functionality.
这就需要切换到Python3的版本了。
输入如下命令,查看Python信息
mac@mac ~ % which python
/usr/local/bin/python
mac@mac ~ % /usr/local/bin/python '-V'
Python 2.7.17
mac@mac ~ % which python3
/usr/local/bin/python3
mac@mac ~ % /usr/local/bin/python3 '-V'
Python 3.8.0
终端查看Python的sys.path
python2
结果
Python 2.7.17 (v2.7.17:c2f86d86e6, Oct 19 2019, 16:07:15)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
终端继续输入
import sys
print sys.path
结果
>>> import sys
>>> print sys.path
['', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
同理对于python3如下
mac@mac ~ % python3
Python 3.8.0 (v3.8.0:fa919fdf25, Oct 14 2019, 10:23:27)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print (sys.path)
['', '/Library/Frameworks/Python.framework/Versions/3.8/lib/python38.zip', '/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8', '/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/lib-dynload', '/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages']
>>>
以上得到了Python2和Python3的路径。
接下来更改配置文件
vi ~/.bash_profile
点击i进入编辑模式,在配置文件中进行如下配置,如果已经配置好,跳过此步。
# Setting PATH for Python 3.8
# The original version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.8/bin:${PATH}"
export PATH
alias python="/Library/Frameworks/Python.framework/Versions/3.8/bin/python3.8"
# Setting PATH for Python 2.7
# The original version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
export PATH
编辑好后,点 :wq 保存退出。
重新打开一个终端,当需要切换到Python3版本时,执行如下命令
source ~/.bash_profile
此时在终端继续
python
就会发现,已经切换到Python3了,结果如下
mac@mac ~ % source ~/.bash_profile
mac@mac ~ % python
Python 3.8.0 (v3.8.0:fa919fdf25, Oct 14 2019, 10:23:27)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
注意: 当关闭了该终端,Mac又会默认为Python2,所以,当想用Python3时,需要每次在新的终端输入
source ~/.bash_profile
最后
以上就是粗暴电话为你收集整理的Mac中切换Python版本,Python2和Python3之间的切换的全部内容,希望文章能够帮你解决Mac中切换Python版本,Python2和Python3之间的切换所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复