which python
输出结果 不同设备可能不同 /usr/bin/python在shell中逐行输入下面命令 来调用python并查看python解释器和模块库包的调用路径
python3 import sys sys.path
输出结果 不同设备结果不同 [ /Library/Developer/CommandLineTools/Library/frameworks/Python3.framework/Versions/3.8/lib/python38.zip , /Library/Developer/CommandLineTools/Library/frameworks/Python3.framework/Versions/3.8/lib/python3.8 , /Library/Developer/CommandLineTools/Library/frameworks/Python3.framework/Versions/3.8/lib/python3.8/lib-dynload , /Library/Developer/CommandLineTools/Library/frameworks/Python3.framework/Versions/3.8/lib/python3.8/site-packages ]
此时 如有需要 可以根据这些路径删除该python版本以及模块文件夹来节省内存
接下来 修改默认python版本 打开.bash_profile文件 这是终端shell的环境变量配置文件 每次启动终端shell都会自动运行它
vi ~/.bash_profile如果不存在 先创建 再打开
touch ~/.bash_profile vi ~/.bash_profile输入i开启写入命令 假如你的新python解释器所在文件夹是/Users/liudong/opt/anaconda3/bin 那么输入
export PATH ${PATH}:/Users/liudong/opt/anaconda3/bin
alias python /Users/liudong/opt/anaconda3/bin/python
然后按下ESC键 并输入:wq! 保存并退出。然后重启终端shell或输入以下命令执行~/.bash_profile使其生效
source ~/.bash_profile成功。
假如失败的解决方法 如果source成功 但是重启shell后又失效了 很可能是~/.bash_profile文件重启失效。因为终端shell启动时先加载 ~/.zshrc后就不加载其他环境变量配置文件了。 解决方法 在~/.zshrc文件最后 增加一行 source ~/.bash_profile



