python2.7源碼安裝方式

安裝python2.7

  1. 下載Python 2.7, 下載地址
  2. 解壓安裝
tar -xzvf Python-2.7.15.tgz
cd Python-2.7.15

./configure --prefix=/opt/local/python --enable-shared
make && make install
  1. 配置環境變量
vim ~/.bash_profile

編輯內容python

PYHOME=/opt/local/python
export PATH=$PYHOME/bin:$PATH

導入變量linux

source ~/.bash_profile
  1. 查看版本號
python --version
  1. 安裝setuptools

下載setuptools 安裝包, 下載地址git

tar -xzvf setuptools-39.2.0.tgz
cd setuptools-39.2.0
python setup.py install
  1. 安裝pip
    下載pip 安裝包, 下載地址
tar -xzvf pip-10.0.1.tgz
cd pip-10.0.1
python setup.py install

FAQ

提示readline找不到

報錯信息github

Python 2.7.15 (default, May 29 2018, 13:09:02)
[GCC 4.3.4 [gcc-4_3-branch revision 152973]] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Traceback (most recent call last):
  File "/etc/pythonstart", line 7, in <module>
    import readline
ImportError: No module named readline

解決方法
安裝readline,從pipy網站下載源碼vim

tar -xzvf readline-6.2.4.1.tgz
cd readline-6.2.4.1
python setup.py install

執行python setup.py 提示md5找不到

報錯信息bash

kwe1000570040:/opt/local/soft/python/setuptools-39.2.0 # python setup.py install
ERROR:root:code for hash md5 was not found.
Traceback (most recent call last):
  File "/opt/local/python/lib/python2.7/hashlib.py", line 147, in <module>
    globals()[__func_name] = __get_hash(__func_name)
  File "/opt/local/python/lib/python2.7/hashlib.py", line 97, in __get_builtin_constructor
    raise ValueError('unsupported hash type ' + name)
ValueError: unsupported hash type md5
ERROR:root:code for hash sha1 was not found.

解決方法
該錯誤是因爲python找不到openssl致使,若是openssl安裝位置有變化可能會致使該問題。
python2.7依賴於openssl_1.0.0,須要下載源碼安裝:python2.7

wget https://www.openssl.org/source/openssl-1.0.2o.tar.gz
tar -xzvf openssl-1.0.2o
cd openssl-1.0.2o
./config shared 
make && make install

openssl默認會安裝到 /usr/local/ssl目錄中,其中lib目錄存放了libssl.so.1.0.0動態連接庫。
接下來是將lib目錄添加到ldconfig路徑:網站

echo "/usr/local/ssl/lib" >> /etc/ld.so.conf
ldconfig

再次執行python程序,問題解決。ui


若是但願更改openssl的安裝路徑,能夠執行prefix參數(安裝目錄)、openssldir參數(配置、證書路徑).net

./config shared --prefix=/usr/local/openssl --openssldir=/usr/local/openssl

更多參數可參考這裏

提示libpython2.7.so.1.0 找不到

在import 某些庫的時候報錯:

ImportError: libpython2.7.so.1.0: cannot open shared object file: No such file or directory

解決方法
須要讓ldconfig找到libpython動態連接庫

echo "/opt/local/python/lib" >> /etc/ld.so.conf
ldconfig

參考文檔

linux ldconfig配置
openssl安裝

相關文章
相關標籤/搜索