操作系统环境: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 -zxvf 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.