1. 程式人生 > >python2.7.13環境搭建

python2.7.13環境搭建

possible 裝包 import tar.gz 安裝 fig 官網 出錯 port

查看當前系統中的 Python 版本,可以看到實驗室的這臺服務器已經安裝了 Python 2.6.6

python --version

檢查 CentOS 版本,我們可以看到這臺服務器的 CentOS的版本是 CentOS release 6.8

cat /etc/redhat-release

為了避免後續安裝出錯,我們先來安裝開發工具包 先安裝 Development Tools yum groupinstall -y "Development tools" 然後安裝其它的工具包 yum install -y zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel 下載、編譯和安裝 Python 2.7.13 yum
源中沒有新版 Python ,我們到官網中下載 Python 2.7.13 wget https://mc.qcloudimg.com/static/archive/b577469e4ed03782eb1f62e8fd6125a5/Python-2.7.13.tar.gz 下載完成後,解壓這個安裝包 tar zxvf Python-2.7.13.tar.gz 進入文件夾 Python-2.7.13 cd Python-2.7.13 執行 configure 文件預編譯 ./configure 編譯和安裝 make && make install

配置 Python

更新系統默認 Python 版本

先把系統默認的舊版 Python 重命名 mv /usr/bin/python /usr/bin/python.old 再刪除系統默認的 python-config 軟鏈接 rm -f /usr/bin/python-config 最後創建新版本的 Python 軟鏈接 ln -s /usr/local/bin/python /usr/bin/python ln -s /usr/local/bin/python-config /usr/bin/python-config ln -s /usr/local/include/python2.7/ /usr/include/python2.7 編輯 /usr/bin/yum 文件,把代碼第一行的 python 改為指向老的 python2.6 版本,修改內容參考以下:
#!/usr/bin/python2.6
import sys try: import yum except ImportError: print >> sys.stderr, """\nThere was a problem importing one of the Python modules required to run yum. The error leading to this problem was: %s Please install a package which provides this module, or verify that the module is installed correctly. Its possible that the above module doesnt match the current version of Python, which is: %s If you cannot solve this problem yourself, please go to the yum faq at: http://yum.baseurl.org/wiki/Faq """ % (sys.exc_value, sys.version) sys.exit(1) sys.path.insert(0, /usr/share/yum-cli) try: import yummain yummain.user_main(sys.argv[1:], exit_code=True) except KeyboardInterrupt, e: print >> sys.stderr, " Exiting on user cancel." sys.exit(1)

再查看 Python 版本,現在我們看到的已經是最新版了

python --version

為新版 Python 安裝一些工具

為新版 Python 安裝 pip

curl https://bootstrap.pypa.io/get-pip.py | python 使用 pip 安裝第三方庫 requests pip install requests 測試: 編寫一個hello.py 文件
#!/usr/bin/python
 
print "Hello, World!";

執行 python hello.py

python2.7.13環境搭建