编译可移植的python

时间:2021-09-18 07:22:23
1. 在低版本系统机器上,编译python:
cd Python-2.7.15/Modules
vim Setup.dist
修改下述代码:
###########
sed -i 's/#SSL=\/usr\/local\/ssl/SSL=\/usr\/local\/ansible_python\/openssl_for_python/g' Modules/Setup.dist
sed -i 's/#_ssl _ssl.c/_ssl _ssl.c/g' Modules/Setup.dist
sed -i 's/#\t-DUSE_SSL/\t-DUSE_SSL/g' Modules/Setup.dist
sed -i 's/#\t-L$(SSL)/\t-L$(SSL)/g' Modules/Setup.dist
sed -i 's/#_md5 md5module.c md5.c/_md5 md5module.c md5.c/g' Modules/Setup.dist
sed -i 's/#zlib zlibmodule.c/zlib zlibmodule.c/g' Modules/Setup.dist
sed -i 's/#binascii binascii.c/binascii binascii.c/g' Modules/Setup.dist
sed -i 's/#_sha1 sha1module.c/_sha1 sha1module.c/g' Modules/Setup.dist
sed -i 's/#_sha256 sha256module.c/_sha256 sha256module.c/g' Modules/Setup.dist
sed -i 's/#_sha512 sha512module.c/_sha512 sha512module.c/g' Modules/Setup.dist
############
2. 指定openssl库
编译新的openssl库
cd openssl-1.0.2o
./configure --prefix=/usr/local/ansible_python/openssl_for_python
make
make install
3. 由于python编译的时候需要去/usr/local/ssl/目录下寻找openssl库(具体可看setup.py),所以需要创建新编译openssl的软链接
ln -s /usr/local/ansible_python/openssl_for_python /usr/local/ssl
4. cd Python-2.7.15
./configure –prefix=/usr/local/ansible_python/python
make
make install
安装完成后,可以安装anshible等模块,方便移植
5. 将自己编译的openssl库放进系统共享库
echo “/usr/local/ansible_python/openssl_for_ansible/lib” >> /etc/ld.so.conf.d/ansible.conf
ldconfig
6. 压缩编译好的python和openssl,以便于移植
tar -cvjf ansible_python.tar.bz2 ansible_python
7. 将压缩包拷贝到新机器上/tmp目录后,解压 /tmp/ansible_python.tar.bz2
tar -xvjf /tmp/ansible_python.tar.bz2 得到 /tmp/ansible_python
8. 拷贝文件
cp -r /tmp/ansible_python /usr/local/ 9. 修改权限,使普通用户具有执行权限
chown -R ops:ops /usr/local/ansible_python/openssl_for_ansible/
chmod 755 /usr/local/ansible_python/python/bin/* 10. 创建指定的ssl库的软链
ln -s /usr/local/ansible_python/openssl_for_ansible/ /usr/local/ssl 11. 将指定的ssl库添加到系统共享库
echo “/usr/local/ansible_python/openssl_for_ansible/lib” >> /etc/ld.so.conf.d/ansible.conf 12. 执行命令
ldconfig
13. 此时新python已经可以使用
/usr/local/ansible_python/python/bin/python