centos7 安裝Python3並配置環境變數親測有效
python官網(linux下載地址、版本可自選):https://www.python.org/ftp/python/
一、新增阿里雲第三方擴充套件源倉庫(安裝擴充源倉庫才能安裝yum 安裝 openssl11 openssl11-devel,python原始碼編譯必須要openssl11 或以上版本)
備註,我這裡是把pyhton安裝包放在 /usr/local/目錄下
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum makecache
如果執行yum makecache這條命令出現很多Errot 503 還有 Error 404說明沒執行成功,如下
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
或者curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
需要更換yum映象源後再次執行yum makecache命令
Tips:複製命令的時候提前把撰寫視窗開啟(檢視–撰寫–撰寫視窗),先複製到撰寫視窗裡面,再複製到命令列不容易出錯
二、安裝依賴
yum -y install openssl-devel openssl11 openssl11-devel libffi-devel
yum -y install wget zlib zilb-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make xz-devel libdb-devel
****資料儲存壓縮模組依賴:xz-devel 可以不安裝,資料庫模組依賴:xz-devel 可以不安裝****
三、設定編譯flags以便使用最新的openssl庫 --Tips 都沒有輸出資料
export CFLAGS=$(pkg-config --cflags openssl11)
export LDFLAGS=$(pkg-config --libs openssl11)
驗證變數配置命令1:echo $CFLAGS
驗證變數配置命令2:echo $LDFLAGS
四、配置安裝python3,這裡以Python-3.10.4為例,其它版本也一樣
# 如果要和django版本匹配就必須安裝python3.6.0版本,在/usr/local目錄下執行
# wget https://www.python.org/ftp/python/3.6.8/Python-3.6.8.tgz
wget https://www.python.org/ftp/python/3.10.4/Python-3.10.4.tgz
tar -xzvf Python-3.10.4.tgz
cd Python-3.10.4
以命令逐條執行,以便觀察構建、編譯、安裝過程中是否有報錯,缺少依賴。
#./configure --prefix=/usr/local/Python3.6.8 --enable-shared
./configure --prefix=/usr/local/Python3.10.4 --enable-shared
make
make install
##--enable-share 編譯配置中新增共享庫,否則使用python3 命令時系統會提示 無法找到 libpython3.9.so.1.0 的錯誤
安裝成功輸出如下:
Installing collected packages: setuptools, pip
WARNING: The scripts pip3 and pip3.10 are installed in '/usr/local/python3.10.4/bin' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed pip-22.0.4 setuptools-58.1.0
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
五、配置python環境變數
檢視python3根目錄安裝路徑(以安裝成功的上圖中的地址為準)
配置環境變數
Python3.10版本
export PATH=/usr/local/python3.10.4/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/python3.10.4/lib:$LD_LIBRARY_PATH
Python3.6版本
export PATH=/usr/local/Python3.6.8/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/Python3.6.8/lib:$LD_LIBRARY_PATH
上面是臨時加入,永久加入環境變數需要手動加入
進行編輯: vi /etc/profile
然後複製上面2行後,進行儲存
執行立即生效
source /etc/profile
1
驗證python3 是否可用
[root@VM-node1 ~]# python3
Python 3.10.4 (main, Oct 9 2023, 17:40:07) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
Type "help", "copyright", "credits" or "license" for more information.
驗證pip3 命令是否可用
pip3 -V
pip 22.3 from /usr/local/python3.10.0/lib/python3.10/site-packages/pip (python 3.10)
六、更新pip設定為阿里源
pip3 config set global.index-url https://mirrors.aliyun.com/pypi/simple
pip3 config set install.trusted-host mirrors.aliyun.com
七、更新pip源到新版本
python3 -m pip install --upgrade pip
# 返回Successfully uninstalled pip更新成功
Collecting pip
Downloading https://mirrors.aliyun.com/pypi/packages/a4/6d/6463d49a933f547439d6b5b98b46af8742cc03ae83543e4d7688c2420f8b/pip-21.3.1-py3-none-any.whl (1.7MB)
100% |████████████████████████████████| 1.7MB 4.2MB/s
Installing collected packages: pip
Found existing installation: pip 18.1
Uninstalling pip-18.1:
Successfully uninstalled pip