搭建 Python 開發環境

來源:互聯網
上載者:User

標籤:python   python2.6   python2.73   get-pipe   pip   

準備工作

Python是一種解釋型、物件導向、動態資料類型的進階程式設計語言。首先我們來看看系統中是否已經存在 Python ,並安裝一些開發套件:

安裝前準備


查看當前系統中的 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

下載、編譯和安裝 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/pythonln -s /usr/local/bin/python-config /usr/bin/python-configln -s /usr/local/include/python2.7/ /usr/include/python2.7


編輯 /usr/bin/yum 檔案,把代碼第一行的 python 改為指向老的 python2.6 版本,修改內容參考以下:

yum

#!/usr/bin/python2.6import systry:    import yumexcept ImportError:    print >> sys.stderr, """\nThere was a problem importing one of the Python modulesrequired to run yum. The error leading to this problem was:   %sPlease install a package which provides this module, orverify that the module is installed correctly.It‘s possible that the above module doesn‘t match thecurrent version of Python, which is:%sIf you cannot solve this problem yourself, please go tothe 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


本文出自 “實踐出真知” 部落格,請務必保留此出處http://m51cto.blog.51cto.com/53087/1959189

搭建 Python 開發環境

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.