如何删除macOS系统默认的Python2.7并替换成最新版python3.11

By 刘志军 , 2023-04-15, 分类: 思考

python

MacBook 自带的Python版本是Python2.7,可这个版本早就被淘汰了。我想彻底删除它

~ python
Python 2.7.15 (default, Nov 27 2018, 21:24:58)
[GCC 4.2.1 Compatible Apple LLVM 10.0.0 (clang-1000.11.45.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

在终端输入python,显示版本是Python 2.7.15

再看python命令在什么位置

~ where python
/usr/local/bin/python

注意该路径其实是一个软链接(类似于Windows系统的快捷方式),通过ls命令查看真实路径是在哪

~ ls -l /usr/local/bin/python
/usr/local/bin/python -> ../Cellar/python@2/2.7.15_1/bin/python

查到python2.7的真实路径是

/usr/local/Cellar/python@2/2.7.15_1/bin/python

不只是python这一个命令有设置了软连接,其实还有好多命令

ls -l /usr/local/bin | grep ../Cellar/python@2/2.7.15_1/bin/python
lrwxr-xr-x  1 wuchangfang  admin        38 May 27  2021 python -> ../Cellar/python@2/2.7.15_1/bin/python
lrwxr-xr-x  1 wuchangfang  admin        45 Dec  8  2018 python-config -> ../Cellar/python@2/2.7.15_1/bin/python-config
lrwxr-xr-x  1 wuchangfang  admin        39 Dec  8  2018 python2 -> ../Cellar/python@2/2.7.15_1/bin/python2
lrwxr-xr-x  1 wuchangfang  admin        46 Dec  8  2018 python2-config -> ../Cellar/python@2/2.7.15_1/bin/python2-config
lrwxr-xr-x  1 wuchangfang  admin        41 Dec  8  2018 python2.7 -> ../Cellar/python@2/2.7.15_1/bin/python2.7
lrwxr-xr-x  1 wuchangfang  admin        48 Dec  8  2018 python2.7-config -> ../Cellar/python@2/2.7.15_1/bin/python2.7-config
lrwxr-xr-x  1 wuchangfang  admin        39 Dec  8  2018 pythonw -> ../Cellar/python@2/2.7.15_1/bin/pythonw
lrwxr-xr-x  1 wuchangfang  admin        40 Dec  8  2018 pythonw2 -> ../Cellar/python@2/2.7.15_1/bin/pythonw2
lrwxr-xr-x  1 wuchangfang  admin        42 Dec  8  2018 pythonw2.7 -> ../Cellar/python@2/2.7.15_1/bin/pythonw2.7

通通把软连接全部删掉

ls -l /usr/local/bin | grep '../Cellar/python@2/2.7.15_1' | awk '{print $9}' | tr -d @ | xargs rm

把python2.7整个目录也删掉

rm -rf /usr/local/Cellar/python@2/2.7.15_1

安装最新的python3.11

打开官网 https://python.org , 鼠标移动到 Downloads, 直接点击 Python3.11.3 下载 安装。

image-20230416164631779

按照提示安装成功后在终端命令行输入python3.11确认是否安装成功

 python3.11
Python 3.11.3 (v3.11.3:f3909b8bc8, Apr  4 2023, 20:12:10) [Clang 13.0.0 (clang-1300.0.29.30)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

如果只想输入python命令就可以进入python解释器,你可以设置一个软链接

先查找python3.11的位置

➜ where python3.11
/usr/local/bin/python3.11

设置软连接

ln -s -f /usr/local/bin/python3.11 /usr/local/bin/python

测试

python --version
Python 3.11.3

本文完


关注公众号「Python之禅」,回复「1024」免费获取Python资源

python之禅

猜你喜欢

2024-03-04
Python中的 if __name__ == '__main__' 是什么?
2024-03-04
用 Python 破解隔壁老王家的 Wi-Fi 密码,刺激!
2023-06-12
Python3.12新特性
2023-04-17
Python虚拟环境使用
2022-12-09
python 中return和yield有什么区别
2022-08-17
如何利用多态干掉 if else 语句
2022-07-22
10个python初学者常犯的错误
2022-06-10
flask-siwadoc 支持openapi 分组功能
2022-06-10
在Python应用中Telegram 机器人搭建消息提醒
2022-05-21
为啥用 PIP 安装库也会提示UnicodeDecodeError?