Python2.6升级Python2.7:
编译环境准备
yum groupinstall "Development tools"
yum install python-devel zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel
下载安装2.7
tar -jxvf Python-2.7.11.tgz
cd Python-2.7.11
./configure --prefix=/usr/local/python27 --enable-shared
make
make install
修改系统python版本
mv /usr/bin/python /usr/bin/python2.6 #备份2.6
ln -s /usr/local/python27/bin/python /usr/bin/python
修改yum脚本/usr/bin/yum: #!/usr/bin/python 改为 #!/usr/bin/python2.6
sed -i '1s/python/python2.6/' /usr/bin/yum
使用python27再次安装setuptools和pip
修改pip版本
sudo mv /usr/bin/pip /usr/bin/pip2.6
sudo ln -s /usr/local/python27/bin/pip /usr/bin/pip
安装常用工具
pip install unp # 一个万能解压工具,依赖click,编译安装最好先装它省去敲不同的解压命令
pip install virtualenv # 如果要装virtualenvwrapper 需要依赖pbr
pip install supervisor # 依赖meld3
pip install ipython ptpython ptipython
安装依赖包:
virtualenv .venv
. .venv/bin/activate
pip install -r requirements.txt
==============================================
遇到的问题:
编译时使用./configure --prefix=/usr/local/python27,没有加--enable-shared参数make的时候没有任何模块build失败,但是不会生成libpython2.7.so.1.0,
在supervisorctl start的时候会报错:libpython2.7.so.1.0: cannot open shared object file。
但是如果加上这个参数会有超级多的module build失败。
解决方法:
重新编译,加上--enable-shared参数,make执行后会在当前目录生成该文件,不执行make install。
然后将它复制到/usr/local/python27/lib下,在/etc/ld.so.conf中添加:/usr/local/python27/lib
保存后运行:ldconfig 使用ldconfig -v|grep python查看如果没有错误就行了.
./configure --prefix=/usr/local/python27 --enable-shared
make
cp libpython2.7.so.1.0 /usr/local/python27/lib
echo '/usr/local/python27/lib' > /etc/ld.so.conf
ldconfig
ldconfig -v
ldconfig -v| grep python
另外一台机器遇上最坏的情况是已经安装了openssl-devel无论怎么样都没法build _hashlib和_ssl,有或没有--enable-shared都提示
Failed to build these modules:
_hashlib _ssl
修改Module/Setup的ssl配置在make的时候,报各种错误指针的信息,无法make成功,openssl都是默认标准安装路径。错误信息:
gcc -pthread -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I. -IInclude -I./Include -DPy_BUILD_CORE -DUSE_SSL -I/usr/local/ssl/include -I/usr/local/ssl/include/openssl -c ./Modules/_ssl.c -o Modules/_ssl.o
./Modules/_ssl.c: In function ‘newPySSLObject’:
./Modules/_ssl.c:307: warning: ‘TLSv1_method’ is deprecated (declared at /usr/local/include/openssl/ssl.h:1596)
./Modules/_ssl.c:309: warning: implicit declaration of function ‘SSLv3_method’
./Modules/_ssl.c:309: warning: passing argument 1 of ‘SSL_CTX_new’ makes pointer from integer without a cast
/usr/local/include/openssl/ssl.h:1302: note: expected ‘const struct SSL_METHOD *’ but argument is of type ‘int’
./Modules/_ssl.c:312: warning: implicit declaration of function ‘SSLv2_method’
./Modules/_ssl.c:312: warning: passing argument 1 of ‘SSL_CTX_new’ makes pointer from integer without a cast
/usr/local/include/openssl/ssl.h:1302: note: expected ‘const struct SSL_METHOD *’ but argument is of type ‘int’
./Modules/_ssl.c: In function ‘_create_tuple_for_X509_NAME’:
./Modules/_ssl.c:613: error: dereferencing pointer to incomplete type
./Modules/_ssl.c:630: error: dereferencing pointer to incomplete type
./Modules/_ssl.c: In function ‘_get_peer_alt_names’:
./Modules/_ssl.c:732: error: dereferencing pointer to incomplete type
./Modules/_ssl.c:736: error: dereferencing pointer to incomplete type
./Modules/_ssl.c:741: error: dereferencing pointer to incomplete type
./Modules/_ssl.c:802: warning: ‘ASN1_STRING_data’ is deprecated (declared at /usr/local/include/openssl/asn1.h:553)
./Modules/_ssl.c: In function ‘PySSL_cipher’:
./Modules/_ssl.c:1140: warning: assignment discards qualifiers from pointer target type
./Modules/_ssl.c: In function ‘PySSL_RAND_egd’:
./Modules/_ssl.c:1610: warning: implicit declaration of function ‘RAND_egd’
make: *** [Modules/_ssl.o] Error 1
这两个模块会导致ssl和urllib2模块没法用,搞了很久都无法很好的解决,最后采用了在另外一台机器上能够正确编译好的_ssl.o和_hashlib.o,拷贝到编译失败的机器上发现可以成功解决,前提是Python大版本要一样,Python2.6不能用于2.7,2.7.11的可以用于2.7.9。
在成功编译Python2.7的机器上找到_ssl.so和_hashlib.so:
find / -iname _ssl.so
/usr/local/python27/lib/python2.7/lib-dynload/_ssl.so
/usr/lib64/python2.6/lib-dynload/_ssl.so
/data/home/ashinchen/pkg/Python-2.7.11/build/lib.linux-x86_64-2.7/_ssl.so
find / -iname _hashlib.so
/usr/local/python27/lib/python2.7/lib-dynload/_hashlib.so
/usr/lib64/python2.6/lib-dynload/_hashlib.so
/data/home/ashinchen/pkg/Python-2.7.11/build/lib.linux-x86_64-2.7/_hashlib.so
将/usr/local/python27/lib/python2.7/lib-dynload/_ssl.so和/usr/local/python27/lib/python2.7/lib-dynload/_hashlib.so复制到失败的机器上对应的lib-dynload目录:
cp _ssl.so _hashlib.so /usr/local/python27/lib/python2.7/lib-dynload/
启动python2.7,import ssl和import urllib2应该已经不会再报错了。
同样如果libpython2.7.so.1.0在本机没有,也可以去其他机器找个对应版本的libpython2.7.so.1.0复制过来就行
装iPython5.1依赖包不是一般多,其中有个叫backports.shutil_get_terminal_size的包,pypi上搜索需要用全名,并不是想象的backports