Centos 安装 python3.x 为默认|全球热头条

2023-04-25 09:56:56 来源:博客园


(资料图片)

CentOS 7 中默认安装了 Python,但是版本是2.x的,由于2020年python2.x将停止更新,因此需要将版本升级至3.x。但由于python2.x是系统集成的,很多命令都是要基于python2.x,比如:yum。因此在更新 Python 时,建议新旧版本共存。

当前初始环境
# 该系统为最小化安装[root@TestServer ~]# cat /etc/redhat-release CentOS Linux release 7.9.2009 (Core)[root@TestServer ~]# uname -r3.10.0-1160.el7.x86_64[root@TestServer ~]# pythonPython 2.7.5 (default, Oct 14 2020, 14:45:30) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> [root@TestServer ~]# python -VPython 2.7.5[root@TestServer ~]# whereis pythonpython: /usr/bin/python /usr/bin/python2.7 /usr/lib/python2.7 /usr/lib64/python2.7 /etc/python /usr/include/python2.7 /usr/share/man/man1/python.1.gz
centos7安装python3有两种方式:yum安装,简单方便,但是版本受限源码安装,较yum安装复杂一点,可安装官方任意版本YUM安装自带Python版本:2.7.5安装Python版本:3.6.8
# 安装epel源,python3在默认的源中是没有的[root@TestServer ~]# yum install epel-release -y# 查看python3版本,根据查到python3的版本来安装,随着python版本更新,epel源中的版本也可能会出现更高版本的python[root@TestServer ~]# yum search python3        //看到目前有python3.4,python3.6两个版本可以安装[root@TestServer ~]# yum install python36 -y# 检查安装结果[root@TestServer ~]# rpm -qa | grep python3python3-pip-9.0.3-8.el7.noarchpython3-libs-3.6.8-18.el7.x86_64python3-setuptools-39.2.0-10.el7.noarchpython3-3.6.8-18.el7.x86_64[root@TestServer ~]# rpm -ql python3-3.6.8-18.el7.x86_64/usr/bin/pydoc3/usr/bin/pydoc3.6/usr/bin/python3/usr/bin/python3.6/usr/bin/python3.6m/usr/bin/pyvenv/usr/bin/pyvenv-3.6/usr/share/doc/python3-3.6.8/usr/share/doc/python3-3.6.8/README.rst/usr/share/licenses/python3-3.6.8/usr/share/licenses/python3-3.6.8/LICENSE/usr/share/man/man1/python3.1.gz/usr/share/man/man1/python3.6.1.gz# 设置默认python版本为python-3.6.8[root@TestServer ~]# mv /usr/bin/python /usr/bin/python_bak[root@TestServer ~]# ln -s /usr/bin/python3 /usr/bin/python[root@TestServer ~]# ls -l /usr/bin/python*lrwxrwxrwx  1 root root    16 Apr 24 11:57 /usr/bin/python -> /usr/bin/python3lrwxrwxrwx. 1 root root     9 Oct  8  2022 /usr/bin/python2 -> python2.7-rwxr-xr-x. 1 root root  7144 Oct 14  2020 /usr/bin/python2.7lrwxrwxrwx  1 root root     9 Apr 24 11:30 /usr/bin/python3 -> python3.6-rwxr-xr-x  2 root root 11328 Nov 17  2020 /usr/bin/python3.6-rwxr-xr-x  2 root root 11328 Nov 17  2020 /usr/bin/python3.6mlrwxrwxrwx. 1 root root     7 Oct  8  2022 /usr/bin/python_bak -> python2[root@TestServer ~]# python -VPython 3.6.8[root@TestServer ~]# python2 -VPython 2.7.5[root@TestServer ~]# python3 -VPython 3.6.8# ========== 无法使用yum =============[root@TestServer ~]# yum install net-tools -y  File "/usr/bin/yum", line 30    except KeyboardInterrupt, e:                            ^SyntaxError: invalid syntax# 解决方法,仅修改以下两文件的第一行[root@TestServer ~]# vim /usr/bin/yum#!/usr/bin/python  修改为---> #!/usr/bin/python2[root@TestServer ~]# vim /usr/libexec/urlgrabber-ext-down#!/usr/bin/python  修改为---> #!/usr/bin/python2
另外,系统默认的python2.7是没有pip工具的,需要的话,同样安装pip工具。pip工具也是分python2和python3的,可以共存,下载的包不能共享,即使用pip3下载的包只能在python3中使用,无法在python2.7中使用。
[root@TestServer ~]# yum install python2-pip -y[root@TestServer ~]# mv /usr/bin/pip /usr/bin/pip_bak[root@TestServer ~]# ln -s /usr/bin/pip3 /usr/bin/pip[root@TestServer ~]# ls -l /usr/bin/pip*lrwxrwxrwx 1 root root  13 Apr 24 15:20 /usr/bin/pip -> /usr/bin/pip3-rwxr-xr-x 1 root root 284 Sep  3  2020 /usr/bin/pip2-rwxr-xr-x 1 root root 288 Sep  3  2020 /usr/bin/pip2.7-rwxr-xr-x 1 root root 407 Oct 14  2020 /usr/bin/pip3lrwxrwxrwx 1 root root   9 Apr 24 14:46 /usr/bin/pip-3 -> ./pip-3.6lrwxrwxrwx 1 root root   8 Apr 24 14:46 /usr/bin/pip-3.6 -> ./pip3.6-rwxr-xr-x 1 root root 407 Oct 14  2020 /usr/bin/pip3.6-rwxr-xr-x 1 root root 282 Sep  3  2020 /usr/bin/pip_bak[root@TestServer ~]# pip -Vpip 9.0.3 from /usr/lib/python3.6/site-packages (python 3.6)[root@TestServer ~]# pip2 -Vpip 8.1.2 from /usr/lib/python2.7/site-packages (python 2.7)[root@TestServer ~]# pip3 -Vpip 9.0.3 from /usr/lib/python3.6/site-packages (python 3.6)

注意:Python 2.7.9 + 或 Python 3.4+ 以上版本都自带 pip 工具。

源码安装

Centos7系统恢复快照至初始环境。

自带Python版本:2.7.5安装Python版本:3.9.16

首先去python官网下载页面,下载需要的稳定版python3源码

【https://www.python.org/downloads/source/】【https://www.python.org/ftp/python/】

目前最新版本为:Python 3.11.3

# 安装依赖[root@TestServer ~]# yum install -y openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel libffi-devel wget gcc make# 下载需求版本[root@TestServer ~]# wget https://www.python.org/ftp/python/3.9.16/Python-3.9.16.tgz[root@TestServer ~]# tar -xf Python-3.9.16.tgz[root@TestServer ~]# cd Python-3.9.16[root@TestServer Python-3.9.16]# ./configure --prefix=/usr/local/python3.9[root@TestServer Python-3.9.16]# make && make install# 设置默认python版本为 python-3.9.16[root@TestServer ~]# mv /usr/bin/python /usr/bin/python_bak[root@TestServer ~]# ln -s /usr/local/src/python3.9/bin/python3 /usr/bin/python[root@TestServer ~]# ln -s /usr/local/src/python3.9/bin/python3 /usr/bin/python3[root@TestServer ~]# ls -l /usr/bin/python*lrwxrwxrwx  1 root root   36 Apr 24 16:23 /usr/bin/python -> /usr/local/src/python3.9/bin/python3lrwxrwxrwx. 1 root root    9 Oct  8  2022 /usr/bin/python2 -> python2.7-rwxr-xr-x. 1 root root 7144 Oct 14  2020 /usr/bin/python2.7lrwxrwxrwx  1 root root   36 Apr 24 16:23 /usr/bin/python3 -> /usr/local/src/python3.9/bin/python3lrwxrwxrwx. 1 root root    7 Oct  8  2022 /usr/bin/python_bak -> python2[root@TestServer ~]# python -VPython 3.9.16[root@TestServer ~]# python2 -VPython 2.7.5[root@TestServer ~]# python3 -VPython 3.9.16# ========== 无法使用yum =============[root@TestServer ~]# yum list  File "/usr/bin/yum", line 30    except KeyboardInterrupt, e:                            ^SyntaxError: invalid syntax# 解决方法,仅修改以下两文件的第一行[root@TestServer ~]# vim /usr/bin/yum#!/usr/bin/python  修改为---> #!/usr/bin/python2[root@TestServer ~]# vim /usr/libexec/urlgrabber-ext-down#!/usr/bin/python  修改为---> #!/usr/bin/python2

标签:

相关热词搜索:

[责任编辑:]

最近更新