1.準備編譯環(huán)境
yum -y groupinstall "Development tools"
yum -y install gcc 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
Centos7 以下系統(tǒng)的要注意openssl 版本,不能低于 1.0.2
openssl 如何升級版本粮宛,請看我上一篇文章
# openssl version
OpenSSL 1.0.2o 27 Mar 2018
2.下載Python3.7.2源碼包
(國外源韵卤,下載比較慢)
wget https://www.python.org/ftp/python/3.7.2/Python-3.7.2.tar.xz
3.解壓
tar -xvf Python-3.7.2.tar.xz
4.創(chuàng)建編譯安裝目錄并安裝
需要openssl 版本在 1.0.2 及以上斥季,否則無法導入ssl模塊,pip install 也會報錯
cd Python-3.7.2/
./configure --prefix=/usr/local/python3 --with-openssl=/usr/local/ssl --enable-optimizations(加快代碼運行速度)
make && make install
ln -s /usr/local/python3/bin/python3 /usr/local/bin/
ln -s /usr/local/python3/bin/pip3 /usr/local/bin/
--enable-optimizations 加快代碼運行效率汽摹,會導致編譯時間過長驮履,可忽略.centos6系統(tǒng)不要加這個,編譯會卡著不動
--with-openssl=/usr/local/ssl 指定openssl安裝位置 ,通過yum 安裝openssl >=1.0.2版本的不需要該選項,
5.測試
#python3
Python 3.7.2 (default, Jan 10 2020, 18:08:10)
[GCC 4.8.2 20131212 (Red Hat 4.8.2-8)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ssl
>>>
更換pip為國內源
#mkdir ~/.pip/
#vim ~/.pip/pip.conf
[global]
index-url = https://mirrors.aliyun.com/pypi/simple