Mac下 Python2、Python3 共存

这里写自定义目录标题

已完成配置后效果

可以通过在终端(控制台)命令which python获取到对应的python路径

如下图:
在这里插入图片描述

同时可以通过命令行 pythonpython2python3 进入到对应的python环境

如下图:
在这里插入图片描述

如何配置

1、Mac下已安装了python2(mac系统自带python2.7)、python3
且确认好python的安装路径

安装来源 python默认安装路径
Mac系统自带 /System/Library/Frameworks/Python.framework/Versions/2.7
官网下载包安装 /Library/Frameworks/Python.framework/Versions/2.7
brew安装 /usr/local/Cellar/python

2、配置 .bash_profile 文件
命令行输入 vi ~/.bash_profile
在文件尾部添加以下内容(需根据自己python实际路径添加

# Setting PATH for Python 2.7
# The original version is saved in .bash_profile.pysave
PATH="/System/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
export PATH

# Setting PATH for Python 3.7
# The original version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/3.7/bin:${PATH}"
export PATH

保存,并 source ~/.bash_profile刷新一下

3、配置 ~/.bashrc 文件
命令行输入 vi ~/.bashrc
在文件尾部添加以下内容

alias python2='/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7'
alias python3='/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7'
alias python=python2
#默认python切换至python3
#alias python=python3

保存,并 source ~/.bashrc刷新一下即可

扫描二维码关注公众号,回复: 3905494 查看本文章

猜你喜欢

转载自blog.csdn.net/sonyv/article/details/83185740