Centos7下完美安装部署Python高版本

【1】首先使用 python -V 命令查看一下是否安装python,如下所示:
1
2
[root@kazama ~]$ python -V
Python 2.7.5

由上面命令可以知道,CentOS7默认安装了python2.7.5 ,因为一些命令要用它,比如yum使用的是python2.7.5


【2】然后使用命令 which python 查看一下 Python 可执行文件的位置,可见文件在 /usr/bin/目录下:
1
2
[root@kazama ~]$ which python
/usr/bin/python

【3】通过cd /usr/bin/命令切换到目录下执行 ll python* 命令查看:
1
2
3
4
5
[root@kazama ~]$ cd /usr/bin
[root@kazama ~]$ ll python*
lrwxrwxrwx. 1 root root 7 Jul 28 2018 python -> python2
lrwxrwxrwx. 1 root root 9 Jul 28 2018 python2 -> python2.7
-rwxr-xr-x. 1 root root 7136 Nov 20 2015 python2.7

由上面可知,python 指向的是python2.7,今天我们安装3.10版本,所以python要指向python3.10才行。


【4】通过 cd /usr/bin/ 切换到/usr/bin/目录下,执行 mv python python.bak 命令进行备份。若有过程中用到yum,可以暂时恢复回来!!!,执行 ll python* 命令查看,如下所示:
1
2
3
4
5
6
[root@huanghao-1 bin]$ cd /usr/bin/
[root@huanghao-1 bin]$ mv python python.bak
[root@huanghao-1 bin]$ ll python*
lrwxrwxrwx. 1 root root 9 Jul 28 2018 python2 -> python2.7
-rwxr-xr-x. 1 root root 7136 Nov 20 2015 python2.7
lrwxrwxrwx. 1 root root 7 Jul 28 2018 python.bak -> python2

【5】通过yum命令先安装相关包:
1
2
[root@kazama bin]$ yum install -y zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make libffi-devel
-bash: /usr/bin/yum: /usr/bin/python: bad interpreter: No such file or directory

执行以上命令后,可以报错了,不要慌!!这是因为我们在步骤4中改变了其环境变量,在/usr/bin目录下,通过命令mv python.bak python还原回来即可。再次执行安装命令就正常了。但是安装完后记得备份回去!!!


【6】下载python3.10.9的源码,自己创建一个目录保存源码,我自己放在/home/目录下。通过cd /home/命令切换到目录下,执行wget下载命令:
1
[root@huanghao-1 bin]$ wget https://www.python.org/ftp/python/3.10.9/Python-3.10.9.tar.xz

如果自己系统没有wget命令,则yum安装一下。

1
yum install-y wget

自己想安装什么python3版本,就到 https://www.python.org/getit/这个网站找,只需将以上版本路径替换即可。

(1)如果我这里安装python3.10.9,打开上面网址后如下图所示:

e6411e8fe632448cf1acfabd06dc1f2f.png (1063×623) (raw.githubusercontent.com)

(2)选择对应python版本后,点击“download”后,下拉找到file页面,如下图所示:

fd5db0aa9003e422feb1ec41122bdabf.png (1080×485) (raw.githubusercontent.com)

(3)此时复制链接地址后,即可通过wget + 链接地址进行python源码下载,如下命令所示,这里对应步骤6的命令:

1
wget https://www.python.org/ftp/python/3.10.9/Python-3.10.9.tar.xz

【7】在/home/目录下已经看到下载好的源码,通过 tar -xvJf Python-3.7.0a4.tar.xz 命令进行解压缩。如下所示:
1
2
3
4
[root@huanghao-1 home]# ll
total 19156
-rw-r--r-- 1 root root 19612112 Dec 7 03:01 Python-3.10.9.tar.xz
[root@huanghao-1 home]# tar -xvJf Python-3.10.9.tar.xz

【8】/home/目录下,通过命令 cd Python-3.10.9 切换进入源码包目录。
1
cd Python-3.10.9

【9】编译安装,依次执行以下命令进行:
1
2
./configure prefix=/usr/local/python3.10 --with-ssl
make && make install

等待安装完毕后,通过命令 cd /usr/local/ 切换到/usr/local/目录下,通过ll命令查看,就会有python3.10目录了,如下图所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@huanghao-1 Python-3.10.9]$ cd /usr/local/
[root@huanghao-1 local]$ ll
total 0
drwxr-xr-x. 2 root root 6 Aug 12 2015 bin
drwxr-xr-x. 2 root root 6 Aug 12 2015 etc
drwxr-xr-x. 2 root root 6 Aug 12 2015 games
drwxr-xr-x. 2 root root 6 Aug 12 2015 include
drwxr-xr-x. 2 root root 6 Aug 12 2015 lib
drwxr-xr-x. 2 root root 6 Aug 12 2015 lib64
drwxr-xr-x. 2 root root 6 Aug 12 2015 libexec
drwxr-xr-x 6 root root 52 Mar 26 19:06 python3.10
drwxr-xr-x. 2 root root 6 Aug 12 2015 sbin
drwxr-xr-x. 5 root root 46 Jul 28 2018 share
drwxr-xr-x. 2 root root 6 Aug 12 2015 src
[root@huanghao-1 local]#

【10】将python3.10版本对应的执行文件添加软链接到执行目录下/usr/bin,通过以下命令完成:
1
ln -s /usr/local/python3.10/bin/python3.10 /usr/bin/python

最终结果如下所示:

1
2
3
4
5
6
7
 
[root@huanghao-1 bin]# cd /usr/bin/
[root@huanghao-1 bin]# ll python*
lrwxrwxrwx 1 root root 36 Mar 26 19:11 python -> /usr/local/python3.10/bin/python3.10
lrwxrwxrwx. 1 root root 9 Jul 28 2018 python2 -> python2.7
-rwxr-xr-x. 1 root root 7136 Nov 20 2015 python2.7
lrwxrwxrwx. 1 root root 7 Jul 28 2018 python.bak -> python2

【11】测试是否成功安装,执行 python -V 命令,执行python2 -V,输出如下所示:成功安装python3.10.9版本。
1
2
3
4
[root@huanghao-1 bin]# python -V
Python 3.10.9
[root@huanghao-1 bin]# python2 -V
Python 2.7.5

【12】因为执行yum等命令需要python2版本,所以我们还要修改yum配置及其它相关配置:

(1)执行命令 vi /usr/bin/yum 进行修改,修改内容如下:

#! /usr/bin/python 修改为:

1
#! /usr/bin/python2

(2)执行命令 vi /usr/libexec/urlgrabber-ext-down

把文件里面的 #! /usr/bin/python 也要修改为:

1
#! /usr/bin/python2

(3)执行命令 vi /usr/sbin/firewalld

文件里面的#!/usr/bin/python -Es也要修改为:

1
#!/usr/bin/python2 -Es

【13】建立pip3的软链接,执行以下命令:
1
ln -s /usr/local/python3.10/bin/pip3 /usr/bin/pip3

【14】这样python3版本就安装完成,同时python2也存在,可以通过以下命令进行查看:
1
2
3
4
[root@huanghao-1 bin]# python -V
Python 3.10.9
[root@huanghao-1 bin]# python2 -V
Python 2.7.5

【15】此时发现安装完成后,无法通过pip3命令安装相关模块,如:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 
[root@huanghao-1 ~]# pip3 install Flask
WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/flask/
WARNING: Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/flask/
WARNING: Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/flask/
WARNING: Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/flask/
WARNING: Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/flask/
Could not fetch URL https://pypi.org/simple/flask/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/flask/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) - skipping
ERROR: Could not find a version that satisfies the requirement Flask (from versions: none)
ERROR: No matching distribution found for Flask
WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Could not fetch URL https://pypi.org/simple/pip/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/pip/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) - skipping
WARNING: There was an error checking the latest version of pip.
[root@node1 Python-3.10.9]# pip3 install --upgrade pip
WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Requirement already satisfied: pip in /usr/local/python3.10/lib/python3.10/site-packages (22.3.1)
WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/pip/
WARNING: Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/pip/
WARNING: Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/pip/
WARNING: Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/pip/
WARNING: Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /simple/pip/
Could not fetch URL https://pypi.org/simple/pip/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/pip/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) - skipping
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
WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Could not fetch URL https://pypi.org/simple/pip/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/pip/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.")) - skipping
WARNING: There was an error checking the latest version of pip.

如上出现报错,此时可以按照如下方法进行解决。

(1)在用户家目录下创建相关文件后进行如下编辑:

1
2
[root@huanghao-1 ~]# mkdir -p ~/.pip
[root@huanghao-1 ~]# vi ~/.pip/pip.conf

(2)在上一步编辑文件中,新增如下内容:

1
2
3
4
5
[global]
index-url = http://mirrors.aliyun.com/pypi/simple

[install]
trusted-host = mirrors.aliyun.com

(3)此时再次安装相应模块,如下可以成功了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 
[root@huanghao-1 ~]# pip3 install Flask
Looking in indexes: http://mirrors.aliyun.com/pypi/simple
Collecting Flask
Downloading http://mirrors.aliyun.com/pypi/packages/fa/1a/f191d32818e5cd985bdd3f47a6e4f525e2db1ce5e8150045ca0c31813686/Flask-2.3.2-py3-none-any.whl (96 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 96.9/96.9 kB 1.8 MB/s eta 0:00:00
Collecting Jinja2>=3.1.2
Downloading http://mirrors.aliyun.com/pypi/packages/bc/c3/f068337a370801f372f2f8f6bad74a5c140f6fda3d9de154052708dd3c65/Jinja2-3.1.2-py3-none-any.whl (133 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 133.1/133.1 kB 1.4 MB/s eta 0:00:00
Collecting click>=8.1.3
Downloading http://mirrors.aliyun.com/pypi/packages/1a/70/e63223f8116931d365993d4a6b7ef653a4d920b41d03de7c59499962821f/click-8.1.6-py3-none-any.whl (97 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 97.9/97.9 kB 1.4 MB/s eta 0:00:00
Collecting Werkzeug>=2.3.3
Downloading http://mirrors.aliyun.com/pypi/packages/ba/d6/8040faecaba2feb84e1647af174b3243c9b90c163c7ea407820839931efe/Werkzeug-2.3.6-py3-none-any.whl (242 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 242.5/242.5 kB 1.5 MB/s eta 0:00:00
Collecting blinker>=1.6.2
Downloading http://mirrors.aliyun.com/pypi/packages/0d/f1/5f39e771cd730d347539bb74c6d496737b9d5f0a53bc9fdbf3e170f1ee48/blinker-1.6.2-py3-none-any.whl (13 kB)
Collecting itsdangerous>=2.1.2
Downloading http://mirrors.aliyun.com/pypi/packages/68/5f/447e04e828f47465eeab35b5d408b7ebaaaee207f48b7136c5a7267a30ae/itsdangerous-2.1.2-py3-none-any.whl (15 kB)
Collecting MarkupSafe>=2.0
Downloading http://mirrors.aliyun.com/pypi/packages/12/b3/d9ed2c0971e1435b8a62354b18d3060b66c8cb1d368399ec0b9baa7c0ee5/MarkupSafe-2.1.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (25 kB)
Installing collected packages: MarkupSafe, itsdangerous, click, blinker, Werkzeug, Jinja2, Flask
Successfully installed Flask-2.3.2 Jinja2-3.1.2 MarkupSafe-2.1.3 Werkzeug-2.3.6 blinker-1.6.2 click-8.1.6 itsdangerous-2.1.2
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

[notice] A new release of pip available: 22.3.1 -> 23.2.1
[notice] To update, run: /usr/local/python3.10/bin/python3.10 -m pip install --upgrade pip

(4)根据提示,也可以升级pip版本到最新的版本。

1
pip3 install --upgrade pip

(5)下面可以通过创建一段脚本,验证下载的模块是否可以正常使用,创建名为 hello_flask.py 的文件,内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
 
[root@huanghao-1 ~]# cat hello_flask.py
from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello():
return 'Hello flask'

if __name__ == '__main__':
app.run(host='0.0.0.0',port=8888,debug=True)

(6)执行hello_flask.py脚本

1
python hello_flask.py

(7)当前我的服务器地址是192.168.20.232,在浏览器访问如下地址:得到响应如下图所示:

32f93fdfb5514174a27e7e7b385792cf.png (752×332) (raw.githubusercontent.com)