如何删除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之禅

猜你喜欢

2017-10-22
图解Python变量与赋值
2015-11-25
OS X El-Capitan 安装 virtualenvwrapper 遇到 Operation not permitted
2019-05-12
Python时间戳转日期时间
2019-05-13
Python 在函数中使用全局变量
2015-12-24
Python字典对象实现原理
2019-04-25
命令行神器 Click 快速上手
2014-02-17
Python多线程编程
2015-12-08
Python 整数对象实现原理
2022-06-10
flask-siwadoc 支持openapi 分组功能
2020-06-14
python合并两个列表