您好,登錄后才能下訂單哦!
這篇文章主要講解了“如何編寫Shell腳本實現CentOS 6.x系統升級Python到2.7版本”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“如何編寫Shell腳本實現CentOS 6.x系統升級Python到2.7版本”吧!
在CentOS 6.x上,默認自帶的Python是2.6.x版本,這個版本的Python有點老了,比如“collections.OrderedDict”就是2.7才有的,而且著名的Python Web框架Django的新版(如:1.7)就不支持Python2.6,最低要求是2.7了。而一些公司或者共有云上的服務器就是使用CentOS6.x,所以也就有了升級Python到2.7的需求。
升級Python之前,需要先安裝一些工具和軟件庫,否則后面安裝Python或pip時可能出錯。
Python2.7通過源碼安裝,python可行執行程序默認是安裝在/usr/local/bin/,一般來在$PATH中,/usr/local/bin是優先使用的(如果不是,需要自己設置一下PATH環境變量)。
在安裝完python后,還需要安裝easy_install和pip這兩個最常用工具。
代碼如下:
#!/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 "-------------------------"
感謝各位的閱讀,以上就是“如何編寫Shell腳本實現CentOS 6.x系統升級Python到2.7版本”的內容了,經過本文的學習后,相信大家對如何編寫Shell腳本實現CentOS 6.x系統升級Python到2.7版本這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。