Mac中切换Python版本,Python2和Python3之间的切换

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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值