Python2升级到Python3

操作系统环境:CentOS Linux release 7.4.1708 (Core)。
系统默认Python版本为2.7。

升级前的版本信息:

[root@cch-spider-web1 ~]# ll /usr/bin/python*
lrwxrwxrwx. 1 root root    7 Oct 15  2017 /usr/bin/python -> python2
lrwxrwxrwx. 1 root root    9 Oct 15  2017 /usr/bin/python2 -> python2.7
-rwxr-xr-x. 1 root root 7136 Aug  4  2017 /usr/bin/python2.7

安装Python3:

yum install -y gcc gcc-c++ libffi-devel python-setuptools vim wget make sqlite-devel zlib*  bzip2-devel openssl-devel ncurses-devel readline-devel tk-devel
cd /opt
wget https://mirrors.huaweicloud.com/python/3.9.6/Python-3.9.6.tar.xz
tar -xvf Python-3.9.6.tar.xz
cd /opt/Python-3.9.6
./configure --with-ssl
make
make install

设置默认版本:

mv /usr/bin/python /usr/bin/python.bak
ln -s /usr/local/bin/python3 /usr/bin/python

修复yum配置:

修改/usr/bin/yum/usr/libexec/urlgrabber-ext-down,将#!/usr/bin/python改为#!/usr/bin/python2.7,保存退出即可。

升级pip:

pip3 install --upgrade pip

解决root用户执行pip3命令时提示警告的问题:

[root@cch-spider-web1 ~]# pip3 install --upgrade pip
Looking in indexes: http://mirrors.aliyun.com/pypi/simple/
Requirement already satisfied: pip in /usr/local/lib/python3.9/site-packages (21.1.3)
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv

改为执行时带上--user参数:

pip3 install --user packagename

至此,Python2升级到Python3完毕!

【参考】
https://www.cnblogs.com/cuianbing/p/13433528.html Centos7升级Python
https://blog.csdn.net/weixin_43465312/article/details/105480343 centos7 中python pip3安装报警告提示Try pip3 install --user instead.

posted @   nuccch  阅读(700)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示