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

编辑内容

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

导入变量

source ~/.bash_profile
  1. 查看版本号
python --version
  1. 安装setuptools

下载setuptools 安装包, 下载地址

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找不到

报错信息

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网站下载源码

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

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

报错信息

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,需要下载源码安装:

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程序,问题解决。


如果希望更改openssl的安装路径,可以执行prefix参数(安装目录)、openssldir参数(配置、证书路径)

./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安装

posted @ 2018-05-30 16:23  美码师  阅读(3259)  评论(0编辑  收藏  举报