1. 程式人生 > 實用技巧 >Centos 安裝python3

Centos 安裝python3

安裝步驟

一、檢查是否有 GCC

gcc --version # 檢視gcc版本
# 若沒有輸出,執行安裝命令
yum  -y install gcc

二、安裝依賴

  如果不安裝依賴,在安裝 Python 的時候會報錯

yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
yum -y groupinstall "Development tools
" yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel yum install -y libffi-devel zlib1g-dev yum install zlib* -y

三、下載python

  根據需要下載相關版本的 Python 包,並解壓

wgethttps://www.python.org/ftp/python/3.6.5/Python-3.6.5.tgz
(或者下載在本地上傳到伺服器)
tar -xvJf Python-3.6.5.tgz

四、建立目錄並安裝 Python

# 指定安裝的路徑,不指定的話,安裝過程中可能軟體所需要的檔案複製到其他不同目錄,刪除軟體很不方便,複製軟體也不方便
mkdir /usr/local/python3 
cd Python-3.6.5.tgz
 # 配置,可以提高python10%-20%程式碼執行速度. 
./configure --prefix=/usr/local/python3 --enable-optimizations --with-ssl

# 編譯安裝會一起安裝 pip、ssl等
make && make install

五、建立軟連線

ln -s /usr/local/python3/bin/python3 /usr/local/bin/python3
ln -s /usr/local/python3/bin/pip3 /usr/local/bin/pip3

大功告成

python3 -V
pip3 -V

相關報錯及解決

安裝 Python 時報錯

  • 報錯——zipimport.ZipImportError: can't decompress data; zlib not available Makefile:1099: recipe for target 'install' failed make: *** [install] Error 1
    • 原因:缺少依賴
    • 解決:
      yum -y install zlib1g-dev
  • 報錯——ModuleNotFoundError: No module named '_ctypes'
    • 原因:缺少依賴
    • 解決:
      yum -y install libffi-devel 

pip install 時報錯

報錯——pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available. Could not fetch URL https:*******: There was a problem confirming the ssl certificate: Can't connect to HTTPS URL because the SSL module is not available. - skipping

  原因:在 ./configure 過程中,如果沒有加上 –with-ssl 引數時,預設安裝的軟體涉及到 ssl 的功能不可用,剛好 pip3 過程需要 ssl 模組,而由於沒有指定,所以該功能不可用。解決辦法是重新對 python 進行編譯安裝