在centos 7 32位中编译python2.7.6 ucs2格式
因为一个逆天的环境需要python2和ucs2的情况,在网上看资料都没有很完美的解决方案,如果你不知道当前环境是ucs2还是4用以下方法,打印查看当前python是ucs4还是ucs2
# 65535是ucs2 1114111是ucs4
python -c "import sys; print sys.maxunicode"
安装编译所需依赖
sudo yum install openssl-devel libffi-devel xz-devel -y
sudo yum install bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel -y
sudo yum install ncurses-devel libpcap-devel zlib-devel tk-devel Cython-y
python源码编译
# 下载 这里数字可以换你想要的版本
wget https://www.python.org/ftp/python/2.7.6/Python-2.7.6.tgz
# 解压
tar -xvf Python-2.7.6.tgz
# 安装前的配置 prefix后跟的是python的安装位置
./configure --enable-unicode=ucs2 --enable-shared --prefix=/home/edward/Desktop/python
修改 Modules/Setup 文件
把这三行注释取消
# Socket module helper for SSL support; you must comment out the other
# socket line above, and possibly edit the SSL variable:
#SSL=/usr/local/ssl
_ssl _ssl.c \
-DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
-L$(SSL)/lib -lssl -lcrypto
编译
make && make install
依赖问题
打印环境看看
python -c "import sys; print sys.path"
['','/usr/lib/python27.zip', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/lib/python2.7/site-packages', '/usr/lib/python2.7/site-packages/gtk-2.0']
可以看到尽管编译了新的python,但是它的环境变量还是系统默认的,需要把里面的路径全部替换成刚刚安装的路径,这部分可以写在bashrc里
export MYPYPATH=/home/edward/Desktop/python
export LD_LIBRARY_PATH=$MYPYPATH/lib:$LD_LIBRARY_PATH
export PYTHONPATH=$MYPYPATH/lib/python2.7:$PYTHONPATH
export PYTHONPATH=$MYPYPATH/lib/python2.7/plat-linux2:$PYTHONPATH
export PYTHONPATH=$MYPYPATH/lib/python2.7/lib-tk:$PYTHONPATH
export PYTHONPATH=$MYPYPATH/lib/python2.7/lib-old:$PYTHONPATH
export PYTHONPATH=$MYPYPATH/lib/python2.7/lib-dynload:$PYTHONPATH
export PYTHONPATH=$MYPYPATH/lib/python2.7/site-packages:$PYTHONPATH
export PYTHONPATH=$MYPYPATH/lib/python2.7/site-packages/gtk-2.0:$PYTHONPATH
pip
在root用户的环境下也引入之前依赖的环境变量
# 下载
curl https://bootstrap.pypa.io/pip/2.7/get-pip.py -o get-pip.py
# 安装
python get-pip.pypython get-pip.py --trusted-host pypi.douban.com -i http://pypi.douban.com/simple/
编译pandas
编译pandas会报找不到一些链接库,比如如下的报错
/bin/ld: 找不到 -lpython2.7
collect2: 错误:ld 返回 1
error: command 'gcc' failed with exit status 1
先确认ld去哪里找这个库
sudo ld -lpython2.7 --verbose
# >
==================================================
试图打开 //usr/i686-redhat-linux/lib32/libpython2.7.so 失败
试图打开 //usr/i686-redhat-linux/lib32/libpython2.7.a 失败
试图打开 //usr/local/lib32/libpython2.7.so 失败
试图打开 //usr/local/lib32/libpython2.7.a 失败
试图打开 //lib32/libpython2.7.so 失败
试图打开 //lib32/libpython2.7.a 失败
试图打开 //usr/lib32/libpython2.7.so 失败
试图打开 //usr/lib32/libpython2.7.a 失败
试图打开 //usr/i686-redhat-linux/lib/libpython2.7.so 失败
试图打开 //usr/i686-redhat-linux/lib/libpython2.7.a 失败
试图打开 //usr/local/lib/libpython2.7.so 失败
试图打开 //usr/local/lib/libpython2.7.a 失败
试图打开 //lib/libpython2.7.so 失败
试图打开 //lib/libpython2.7.a 失败
试图打开 //usr/lib/libpython2.7.so 失败
试图打开 //usr/lib/libpython2.7.a 失败
ld: 找不到 -lpython2.7
在之前的python安装文件夹中找到了libpython2.7.a
,所以直接存个链接过去
ln -s /home/edward/Desktop/python/lib/python2.7/config/libpython2.7.a /usr/lib/libpython2.7.a
之后pandas可以正常编译了