CentOS6.5自帶python環境爲2.6,公司的python環境爲2.7.html
爲了不出現之後代碼出現版本差別,因此把自帶的2 .6版本升級到了2.7,過程十分曲折。。。。python
中途遇到的問題和解決方法請點擊:Python安裝時遇到的問題linux
官方下載地址爲:https://www.python.org/downloads/,選擇的版本爲2.7.14。ubuntu
直接使用wget下載到本地:wget https://www.python.org/ftp/python/2.7.14/Python-2.7.14.tgz
python2.7
安裝前要先裝好gcc
、 g++
、和zlib
相關的依賴庫,假設安裝目錄設置爲/usr/local/python27
,安裝步驟以下:spa
# 安裝依賴庫
yum install gcc g++ zlib zlib-devel openssl-devel
## ubuntu命令
# apt-get install gcc g++ zlib1g-dev openssl make
# 解壓
tar -zxvf Python-2.7.14.tgz
# 建立安裝文件夾
mkdir /usr/local/python27
# 編譯安裝
cd Python-2.7.14
./configure --prefix=/usr/local/python27/
make
make install
安裝完成以後,進入到目錄,一共是四個目錄:bin
include
lib
share
code
[root@blog bin]# cd /usr/local/python27/
[root@blog bin]# ll
# 目錄結構
total 16
drwxr-xr-x 2 root root 4096 Sep 23 14:08 bin # bin目錄下放的可執行文件
drwxr-xr-x 3 root root 4096 Sep 23 14:08 include # 頭文件
drwxr-xr-x 4 root root 4096 Sep 23 14:08 lib # 庫文件目錄
drwxr-xr-x 3 root root 4096 Sep 23 14:08 share
[root@blog bin]# cd bin
[root@blog bin]# ./python2.7
Python 2.7.14 (default, Sep 23 2017, 14:06:15)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-18)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> # 進入這裏表示已經安裝成功
建立軟連接算是這裏最關鍵的一步了,若是這裏沒有處理好,之後可能會遇到不少麻煩,動不動就會由於版本的緣由報錯。orm
首先先備份系統默認的python命令,一共有四個相關命令:htm
[root@blog bin]# which python # 查看系統默認python命令的位置
/usr/bin/python
[root@blog bin]# ls /usr/bin/python* -l # 查看全部的python命令
-rwxr-xr-x 2 root root 4864 Aug 18 2016 /usr/bin/python # 和下面的python26 python2.6同樣
lrwxrwxrwx 1 root root 6 Sep 6 18:36 /usr/bin/python2 -> python # 軟連接到python
-rwxr-xr-x. 1 root root 4864 Jul 24 2015 /usr/bin/python26
-rwxr-xr-x 2 root root 4864 Aug 18 2016 /usr/bin/python2.6
python
python2.6
python26
三個文件是同一個文件,不一樣的名字而已,如今要作的就是替換掉這些文件。blog
# 刪掉以前的python python2命令,留下python26和python2.6備用
rm -rf /usr/bin/python /usr/bin/python2
# 建立新的python連接
ln -s /usr/local/python27/bin/python2.7 /usr/bin/python
ln -s /usr/local/python27/bin/python2.7 /usr/bin/python2
ln -s /usr/local/python27/bin/python2.7 /usr/bin/python27
ln -s /usr/local/python27/bin/python2.7 /usr/bin/python2.7
# 輸入python查看是否成功
python
Python 2.7.14 (default, Sep 23 2017, 14:06:15)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-18)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
使用python --version
也能夠查看版本是否替換成功
yum
命令須要用到python2.6
,上面已經把python
指向了python2.7
,此時若是運行yum
將會報錯,具體詳見:升級python2.7後運行yum出現"No module named yum"的解決辦法
在https://pypi.python.org/pypi/setuptools
下載安裝包,能夠複製下載連接使用wget
命令下載。
下載下來後,解壓安裝:
wget https://pypi.python.org/packages/45/29/8814bf414e7cd1031e1a3c8a4169218376e284ea2553cc0822a6ea1c2d78/setuptools-36.6.0.zip#md5=74663b15117d9a2cc5295d76011e6fd1
unzip setuptools-36.6.0.zip
cd setuptools-36.6.0
python setup.py install
下載地址:https://pypi.python.org/pypi/pip
,和上面同樣下載下來,這裏能夠選擇tgz格式的安裝包。
wget https://pypi.python.org/packages/11/b6/abcb525026a4be042b486df43905d6893fb04f05aac21c32c638e939e447/pip-9.0.1.tar.gz#md5=35f01da33009719497f01a4ba69d63c9
tar -zxvf pip-9.0.1.tar.gz
cd pip-9.0.1
python setup.py install
進入到/usr/local/python27/bin
目錄,若是上面安裝沒報錯的話,easy_install
和pip
命令就會出如今這裏了:
建立軟連接:
rm -rf /usr/bin/easy_install* /usr/bin/pip
ln -s /usr/local/python27/bin/pip2.7 /usr/bin/pip
ln -s /usr/local/python27/bin/pip2.7 /usr/bin/pip27
ln -s /usr/local/python27/bin/pip2.7 /usr/bin/pip2.7
ln -s /usr/local/python27/bin/easy_install /usr/bin/easy_install
ln -s /usr/local/python27/bin/easy_install /usr/bin/easy_install27
ln -s /usr/local/python27/bin/easy_install /usr/bin/easy_install2.7
# 驗證操做是否成功
pip --version
pip 9.0.1 from /usr/local/python27/lib/python2.7/site-packages/pip-9.0.1-py2.7.egg (python 2.7)
[root@blog bin]# easy_install --version
setuptools 36.5.0 from /usr/local/python27/lib/python2.7/site-packages/setuptools-36.5.0-py2.7.egg (Python 2.7)
到這裏,全部的安裝過程就完成了。