在CentOS 6.x上,默認(rèn)自帶的Python是2.6.x版本,這個版本的Python有點(diǎn)老了,比如“collections.OrderedDict”就是2.7才有的,而且著名的Python Web框架Django的新版(如:1.7)就不支持Python2.6,最低要求是2.7了。而一些公司或者共有云上的服務(wù)器就是使用CentOS6.x,所以也就有了升級Python到2.7的需求。
升級Python之前,需要先安裝一些工具和軟件庫,否則后面安裝Python或pip時可能出錯。
Python2.7通過源碼安裝,python可行執(zhí)行程序默認(rèn)是安裝在/usr/local/bin/,一般來在$PATH中,/usr/local/bin是優(yōu)先使用的(如果不是,需要自己設(shè)置一下PATH環(huán)境變量)。
在安裝完python后,還需要安裝easy_install和pip這兩個最常用工具。
整個安裝過程,我總結(jié)過如下一個Shell腳本(以安裝Python2.7.8為例,以root權(quán)限運(yùn)行),供參考:
https://github.com/smilejay/shell/blob/master/sh2014/install_py27_on_centos.sh
復(fù)制代碼 代碼如下:
#!/bin/bash
# a script to install python 2.7 on CentOS 6.x system.
# CentOS 6.x has python 2.6 by default, while some software (e.g. django1.7)
# need python 2.7.
# install some necessary tools libs
echo "install some necessary tools libs"
yum groupinstall "Development tools"
yum install openssl-devel zlib-devel ncurses-devel bzip2-devel readline-devel
yum install libtool-ltdl-devel sqlite-devel tk-devel tcl-devel
sleep 5
# download and install python
version='2.7.8'
python_url="https://www.python.org/ftp/python/$version/Python-${version}.tgz"
# check current python version
echo "before installation, your python version is: $(python -V 2>1)"
python -V 2>1 | grep "$version"
if [ $? -eq 0 ]; then
echo "current version is the same as this installation."
echo "Quit as no need to install."
exit 0
fi
echo "download/build/install your python"
cd /tmp
wget $python_url
tar -zxf Python-${version}.tgz
cd Python-${version}
./configure
make -j 4
make install
sleep 5
echo "check your installed python"
python -V 2>1 | grep "$version"
if [ $? -ne 0 ]; then
echo "python -V is not your installed version"
/usr/local/bin/python -V 2>1 | grep "$version"
if [ $? -ne 0 ]; then
echo "installation failed. use '/usr/local/bin/python -V' to have a check"
fi
exit 1
fi
sleep 5
# install setuptools
echo "install setuptools"
wget https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py
python ez_setup.py
# check easy_install version
easy_install --version
sleep 5
# install pip for the new python
echo "install pip for the new python"
easy_install pip
# check pip version
pip -V
echo "Finished. Well done!"
echo "If 'python -V' still shows the old version, you may need to re-login."
echo "And/or set /usr/local/bin in the front of your PATH environment variable."
echo "-------------------------"
您可能感興趣的文章:- CentOS7中使用shell腳本安裝python3.8環(huán)境(推薦)
- Centos7下源碼安裝Python3 及shell 腳本自動安裝Python3的教程
- python3監(jiān)控CentOS磁盤空間腳本
- Centos7 下安裝python3及卸載的教程
- centos 安裝Python3 及對應(yīng)的pip教程詳解
- 關(guān)于CentOs系統(tǒng)自帶python和yum卸載后的解決辦法
- CentOS 7如何實(shí)現(xiàn)定時執(zhí)行python腳本