Python 基礎:python 安裝-Linux源碼安裝


概述

本次在CentOS 7 下完成源碼安裝,並實現python 多版本共存。
[root@localhost ~]# cat /etc/redhat-release
 CentOS Linux release 7.6.1810 (Core)

踩過的坑:

Error1
Could not build the ssl module!
Python requires an OpenSSL 1.0.2 or 1.1 compatible libssl with X509_VERIFY_PARAM_set1_host().
解決辦法:

源碼編譯安裝指定的OpenSSL版本

Error2
zipimport.ZipImportError: can’t decompress data; zlib not available
make: *** [install] Error 1
解決辦法:

[root@localhost Python-3.7.5]# yum install zlib -y

一、安裝前準備

軟件名稱 軟件版本 備註
Python-3.7.5.tgz linux x64 下載
zlib-devel zlib開發工具 鏡像源YUM安裝即可
gcc、gcc-g++ C編譯器 鏡像源YUM安裝即可
make 編譯工具 鏡像源YUM安裝即可

二、安裝過程

1. 安裝依賴環境

[root@localhost Python-3.7.5]#yum install gcc gcc-g++  make  zlib -y

2. 解壓源碼包

[root@localhost Python-3.7.5]# tar -xvf Python-3.7.5.tgz
   # 進入python目錄
[root@localhost Python-3.7.5]# cd Python-3.7.5/

3. 查看安裝說明

[root@localhost Python-3.7.5]# more README.rst

#預編譯安裝到指定安裝目錄

[root@localhost Python-3.7.5]# ./configure --prefix=/opt/python-3.7.5  --enable-optimizations
    --prefix=/opt/python-3.7.5   # 預安裝目錄
    --enable-optimizations    # 穩定性優化

4. 編譯並安裝

[root@localhost Python-3.7.5]# make  # 編譯源碼
[root@localhost Python-3.7.5]#  make test  #測試依賴
[root@localhost Python-3.7.5]# make PREFIX=/opt/python-3.7.5 install   #安裝python

5. 驗證安裝,並添加環境變量

[root@localhost Python-3.7.5]#  /opt/python-3.7.5/bin/python3  # 驗證python3
[root@localhost Python-3.7.5]#  cd /opt/python-3.7.5/bin/
[root@localhost Python-3.7.5]# ./pip3 -version # 驗證pip3 工具
[root@localhost Python-3.7.5]# vim ~/.bashrc
	# python HOME
	export PYTHON3_HOME=/opt/python-3.7.5
	PATH=$PATH:$PYTHON3_HOME/bin
	export PATH
[root@localhost Python-3.7.5]# source  ~/.bashrc #重新加載環境變量,或重新登錄終端
#驗證
[root@localhost ~]# python3
Python 3.7.5 (default, Oct 31 2019, 21:23:03)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

總結:
其中make test 時間相對較長,只要出現的fail 不是很多,就沒啥問題。僅此保留安裝過程,分享下。

相應參考鏈接:
Python 官網
Pypi Package官網

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章