python源码编译(centos6)

最近因为需要用不同gcc版本的python,所以自己手动编了下python,结果遇到一堆问题,所以也记录下自己遇到的问题,和查阅过的文章

Python正常编译流程

首先下载下 python的源代码,选择自己需要的版本即可

比如我就下载了 python3.7.3

1
2
3
4
cd Python-3.7.3
./configure --prefix=/home/python37/ # 注意configure的时候不会生产prefix的文件
make
make install # make install才会把python的相关文件安装到 /home/python37/路径下

理论上如果不出错,这里的python就可以用了。可是现实也不是这么容易(苦笑

下面就是我遇到过的问题,然后对应的解决方案

Python编译问题:Failed to build these modules:

Python编译过程中可能会遇上以下问题,Failed to build xxx modules,虽然可能这些动态库都是存在于系统的,但是python在编译过程中并没找到

1
2
Failed to build these modules:
_sqlite3 _ssl _hashlib

方案一:yum install

如果系统中是缺少相关的库,建议直接yum install 安装一遍,网上有的人说安装完下面的lib就可以成功编译python了,但我自己是没弄成功的

1
2
3
4
5
6
7
8
9
10
yum -y install zlib zlib-devel 
yum -y install bzip2 bzip2-devel
yum -y install ncurses ncurses-devel
yum -y install readline readline-devel
yum -y install openssl openssl-devel
yum -y install openssl-static
yum -y install xz lzma xz-devel
yum -y install sqlite sqlite-devel
yum -y install gdbm gdbm-devel
yum -y install tk tk-devel

如果这个不行,建议手动安装缺少的一些库,就是后面的方案二

方案二:手动编译

编译sqlite3

这个sqlite3缺失,主要方法就是自己下载并编译,然后将其加到python的setup.py中去

  1. sqlite3源码下载链接
  2. sqlite3源码编译,以及python使用

编译openssl

openssl这个很奇怪,我在我的系统中怎么查他都是存在的,但遍python的时候他就是找不到,看下stackoverflow的这个方法就可以解决了

  1. How do I compile Python 3.4 with custom OpenSSL?
  2. Python Build Error: failed to build modules _ssl and _hashlib
  3. 源码编译安装Python3及问题解决

缺少socket

  1. missing _socket after compile python 3.6

自己编译的python安装pip

  1. ImportError: No module named pip

可参考资料:

  1. python编译安装
  2. 编译安装python3