Centos下升級Python
另一篇文章 Centos7安裝Python3.7(兼容Python2.7)http://blog.51cto.com/leyex/2163465
預準備(可忽略)
由於Centos需要提前安裝Sqlite數據庫,否則之後Python無法正常導入sqlite3,因此建議在升級Python前預先安裝Sqlite數據庫
沒有安裝readline-devel可能導致無法使用鍵盤的上下左右鍵
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make -y
一、查看本機Pyhton版本
本機Centos6.5 預帶Pyhton版本爲2.6.6
# python --version >>Python 2.6.6
二、下載欲升級最新Python安裝包
可以去官網找最新安裝包 https://www.python.org/downloads/
三、修改編譯文件支持SSL
編譯安裝的時候並沒有把SSL模塊編譯進去,安裝後可能會導致urllib2不支持https鏈接
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/lib/python2.7/urllib2.py", line 154, in urlopen return opener.open(url, data, timeout) File "/usr/local/lib/python2.7/urllib2.py", line 431, in open response = self._open(req, data) File "/usr/local/lib/python2.7/urllib2.py", line 454, in _open 'unknown_open', req) File "/usr/local/lib/python2.7/urllib2.py", line 409, in _call_chain result = func(*args) File "/usr/local/lib/python2.7/urllib2.py", line 1265, in unknown_open raise URLError('unknown url type: %s' % type) urllib2.URLError: <urlopen error unknown url type: https>
我們需要先安裝openssl-devel(已安裝可忽略)
# rpm -aq|grep openssl
>>openssl-1.0.1e-30.el6.x86_64
# yum install openssl-devel -y
# rpm -aq|grep openssl
>>openssl-1.0.1e-30.el6.x86_64
>>openssl-devel-1.0.1e-42.el6.x86_64
在編譯安裝Python之前加上以下這段代碼(代碼去除註釋即可)
>>vi Python-2.7.11/Modules/Setup.dist
# 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
*另注:
由於系統缺少了readline相關模塊,會導致編譯安裝Python後無法使用上下左右退格,所以需要再編譯安裝前安裝readline-devel模塊
yum -y install readline-devel
四、編譯安裝Python
wget tar -zxvf Python-2.7.10.tgz cd Python-2.7.10 ./configure make && make install #也可以先查看是否有安裝gcc再進行編譯安裝
通過命令查看是否已安裝新版本
# /usr/local/bin/python2.7 -V >>Python 2.7.11
現在我們可以通過python2.7啓動python,但是這樣太麻煩,我們可以自行修改下
mv /usr/bin/python /usr/bin/python2.6.6 ln -s /usr/local/bin/python2.7 /usr/bin/python
# python -V >>Python 2.7.11
五、修改yum腳本環境變量引用
安裝完新版本Python後會發現yum出錯,所以我們要把yum指向老版本
vi /usr/bin/yum
#!/usr/bin/python2.6.6 # 修改#!/usr/bin/python爲#!/usr/bin/python2.6.6 import sys try: import yum except ImportError: print >> sys.stderr, """\ .....
測試yum命令,一切正常
六、升級完Python後安裝pip
python從2.6升級到2.7之後會出現安裝setuptools和pip啓動失敗,這是因爲路徑的問題,我們可以下載一個腳本,運行後會自動重新下載一個setuptools
curl -O https://bootstrap.pypa.io/ez_setup.py
# python ez_setup.py
安裝setuptools
# yum install -y python-setuptools
安裝distribute
由於Setuptools包不再維護distribute,所以安裝後可能由於distribute版本缺失導致報錯
# easy_install Traceback (most recent call last): File "/usr/local/bin/easy_install", line 5, in <module> from pkg_resources import load_entry_point File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 2675, in <module> parse_requirements(__requires__), Environment() File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 552, in resolve raise DistributionNotFound(req) pkg_resources.DistributionNotFound: distribute==0.6.10 distribution was not found and is required by the application
通過以下方式安裝缺失的distribute
# wget https://pypi.python.org/packages/source/d/distribute/distribute-0.6.10.zip --no-check-certificate # unzip distribute-0.6.10.zip # cd distribute-0.6.10 # /usr/local/bin/python2.7 setup.py install
or
# wget # tar xf distribute-0.6.10.tar.gz # cd distribute-0.6.10 # /usr/local/bin/python2.7 setup.py install
安裝pip
# easy_install pip
# easy_install -i https://pypi.tuna.tsinghua.edu.cn/simple pip
七、升級完Python後ibus輸入法報錯
升級 Python2.7 後會導致 Ibus 輸入法python2.6的gtk無法使用問題
/usr/libexec/ibus-ui-gtk /usr/ bin/ibus-setup /usr/libexec/ibus-engine-table
分別修改以上三個文件中的 “python” 爲 “python2.6”
八、配置pip和easy_isntall的鏡像源
easy_install的配置文件 ~/.pydistutils.cfg :
[easy_install] index_url = https://pypi.tuna.tsinghua.edu.cn/simple
pip的配置文件 ~/.pip/pip.conf :
[global] index-url = https://pypi.tuna.tsinghua.edu.cn/simple
至此,升級完畢,後續繼續補充
本文僅作一個記錄,以備忘記查看方便