准備工作
任務時間:5min ~ 10min
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
任務時間:8min ~ 10min
下載、編譯和安裝 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
任務時間:8min ~ 10min
更新系統默認 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/yum
#!/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.
It's possible that the above module doesn't 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 安裝一些工具
任務時間:5min ~ 10min
為新版 Python 安裝 pip
curl https://bootstrap.pypa.io/get-pip.py | python
使用 pip 安裝第三方庫 requests
pip install requests
實驗完成
恭喜,搭建 Python 2.7 開發環境已經完成!