Centos 安裝 python3.x 為預設

2023-04-25 12:03:43

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 -r
3.10.0-1160.el7.x86_64

[root@TestServer ~]# python
Python 2.7.5 (default, Oct 14 2020, 14:45:30) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 
[root@TestServer ~]# python -V
Python 2.7.5
[root@TestServer ~]# whereis python
python: /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 python3
python3-pip-9.0.3-8.el7.noarch
python3-libs-3.6.8-18.el7.x86_64
python3-setuptools-39.2.0-10.el7.noarch
python3-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/python3
lrwxrwxrwx. 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.7
lrwxrwxrwx  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.6m
lrwxrwxrwx. 1 root root     7 Oct  8  2022 /usr/bin/python_bak -> python2

[root@TestServer ~]# python -V
Python 3.6.8
[root@TestServer ~]# python2 -V
Python 2.7.5
[root@TestServer ~]# python3 -V
Python 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/pip3
lrwxrwxrwx 1 root root   9 Apr 24 14:46 /usr/bin/pip-3 -> ./pip-3.6
lrwxrwxrwx 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 -V
pip 9.0.3 from /usr/lib/python3.6/site-packages (python 3.6)
[root@TestServer ~]# pip2 -V
pip 8.1.2 from /usr/lib/python2.7/site-packages (python 2.7)
[root@TestServer ~]# pip3 -V
pip 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原始碼

目前最新版本為: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/python3
lrwxrwxrwx. 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.7
lrwxrwxrwx  1 root root   36 Apr 24 16:23 /usr/bin/python3 -> /usr/local/src/python3.9/bin/python3
lrwxrwxrwx. 1 root root    7 Oct  8  2022 /usr/bin/python_bak -> python2

[root@TestServer ~]# python -V
Python 3.9.16
[root@TestServer ~]# python2 -V
Python 2.7.5
[root@TestServer ~]# python3 -V
Python 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