前段時間浩道分享了一篇關于pycharm專業版本安裝破解后,不少小伙伴后臺咨詢浩道,linux環境下如何安裝更高版本的python3,畢竟系統默認自帶的python2版本已經不符合當前發展潮流,本文浩道就centos7下安裝部署python3.10.9版本帶大家通過以下15個步驟完成,其它版本安裝都可以參照如下步驟進行。福利在文末喔~
步驟1、首先使用 python -V 命令查看一下是否安裝Python,如下所示:
[root@huanghao-1 ~]# python -V
Python 2.7.5
由上面命令可以知道,CentOS7默認安裝了python2.7.5 ,因為一些命令要用它,比如yum 它使用的是python2.7.5。
步驟2、然后使用命令 which python 查看一下Python可執行文件的位置,可見執行文件在/usr/bin/ 目錄下。如下所示:
[root@huanghao-1 ~]# which python
/usr/bin/python
步驟3、通過cd /usr/bin/命令切換到/usr/bin/目錄下執行ll python* 命令查看,如下所示 :
[root@huanghao-1 bin]# cd /usr/bin/
[root@huanghao-1 bin]# 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* 命令查看,如下所示:
[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命令先安裝相關包:
yum install -y zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make libffi-devel
[root@huanghao-1 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還原回來即可。再次執行安裝命令就正常了。但是安裝完后記得備份回去!!!
mv python python.bak
步驟6、下載python3.10.9的源碼,自己創建一個目錄保存源碼,我自己放在/home/目錄下。通過cd /home/命令切換到/home/目錄下,執行wget下載命令:
wget https://www.python.org/ftp/python/3.10.9/Python-3.10.9.tar.xz
如果自己系統沒有wget命令,則yum安裝一下。
yum install-y wget
自己想安裝什么python3版本,就到https://www.python.org/getit/這個網站找,只需將以上版本路徑替換即可。
(1)如我這里安裝python3.10.9,打開上面網址后如下圖所示:
(2)選擇對應python版本后,點擊“download”后,下拉找到file頁面,如下圖所示:
(3)此時復制鏈接地址后,即可通過wget + 鏈接地址進行python源碼下載,如下命令所示,這里對應步驟6的命令:
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命令進行解壓縮。如下所示:
[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切換進入源碼包目錄。
cd Python-3.10.9
步驟9、編譯安裝,依次執行以下命令進行:
./configureprefix=/usr/local/python3.10 --with-ssl
make&&makeinstall
等待安裝完畢后,通過命令cd/usr/local/切換到/usr/local/目錄下,通過ll命令查看,就會有python3.10目錄了,如下圖所示:
[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,通過以下命令完成:
ln-s/usr/local/python3.10/bin/python3.10/usr/bin/python
最終結果如下所示:
[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版本。
[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修改為:
#! /usr/bin/python2
(2)執行命令vi /usr/libexec/urlgrabber-ext-down
把文件里面的#! /usr/bin/python 也要修改為:
#! /usr/bin/python2
(3)執行命令vi /usr/sbin/firewalld
文件里面的#!/usr/bin/python -Es也要修改為:
#!/usr/bin/python2 -Es
步驟13、建立pip3的軟鏈接,執行以下命令:
ln -s /usr/local/python3.10/bin/pip3 /usr/bin/pip3
步驟14、這樣python3版本就安裝完成,同時python2也存在:可以通過以下命令進行查看:
[root@huanghao-1 bin]# python -V
Python 3.10.9
[root@huanghao-1 bin]# python2 -V
Python 2.7.5
步驟15、此時發現安裝完成后,無法通過pip3命令安裝相關模塊,如:
[root@huanghao-1 ~]#pip3installFlask
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)在用戶家目錄下創建相關文件后進行如下編輯:
[root@huanghao-1 ~]# mkdir -p ~/.pip
[root@huanghao-1 ~]# vi ~/.pip/pip.conf
(2)在上一步編輯文件中,新增如下內容:
[global]
index-url = http://mirrors.aliyun.com/pypi/simple
[install]
trusted-host = mirrors.aliyun.com
(3)此時再次安裝相應模塊,如下可以成功了。
[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 000
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 000
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 000
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 000
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版本到最新的版本。
pip3 install --upgrade pip
(5)下面可以通過創建一段腳本,驗證下載的模塊是否可以正常使用,創建名為hello_flask.py的文件,內容如下:
[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腳本
python hello_flask.py
(7)當前我的服務器地址是192.168.20.232,在瀏覽器訪問如下地址:得到響應如下圖所示:
到此整個安裝部署完成,大家可以參考以上步驟自己進行各個python3版本的安裝,注意更改到自己實際環境中的對應目錄即可。
PS:學習python,怎么能少了專業版的開發工具pycharm呢!需要專業破解版可以看看之前發布的這篇文章,三部完美破解,有效期到2099年!
【python開發工具】pycharm專業版安裝破解激活
-
Linux
+關注
關注
87文章
11296瀏覽量
209358 -
python
+關注
關注
56文章
4793瀏覽量
84633
原文標題:【文末送書】Centos7下完美安裝部署python3.10
文章出處:【微信號:浩道linux,微信公眾號:浩道linux】歡迎添加關注!文章轉載請注明出處。
發布評論請先 登錄
相關推薦
評論