Centos 6.8系統鏡像默認安裝的 python 環境是 2.6.6,線上需求須要升級到 2.7.14 版本,網上找了相關資料,升級 python 版本比較容易,但 yum/pip 等命令的使用也會有問題,網上的資料是修改腳本 #!/usr/bin/python 的頭文件,我的感受麻煩,仍是直接升級比較好,不用修改任何系統腳本。python
// 下載python2.7.14並安裝 # wget https://www.python.org/ftp/python/2.7.14/Python-2.7.14.tgz # tar zxvf Python-2.7.14.tgz # cd Python-2.7.14 # ./configure # make # make install // 驗證 # which python /usr/local/bin/python # python -V Python 2.7.14
安裝過程出現了個小插曲,執行which python命令是指向 /usr/local/bin/python 地址的,但執行python進入的確是2.6.6老版本,此時執行 source /etc/profile 生效環境變量,python便可進入正確的環境python2.7
# pip install urllib3 DEPRECATION: Python 2.6 is no longer supported by the Python core team, please upgrade your Python. A future version of pip will drop support for Python 2.6 Requirement already satisfied: urllib3 in /usr/lib/python2.6/site-packages You are using pip version 9.0.1, however version 19.2.2 is available. You should consider upgrading via the 'pip install --upgrade pip' command.
查看pip啓動源碼,發下腳本頭部 #!/usr/bin/python 是使用系統自帶的python命令。ide
# cat /usr/bin/pip #!/usr/bin/python # -*- coding: utf-8 -*- import re import sys from pip import main if __name__ == '__main__': sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0]) sys.exit(main())
經過下面能夠看出pip仍是使用系統的命令,對應的python版本是2.6.6,加載的packages也是2.6.6的,因此沒法安裝urllib3。ui
# which python /usr/local/bin/python # which pip /usr/bin/pip # pip --version pip 9.0.1 from /usr/lib/python2.6/site-packages (python 2.6)
# which python //python使用的是2.7.14 /usr/local/bin/python # python -m pip //pip在python2.7.14找不到 /usr/local/bin/python: No module named pip 其中,-m參數的解釋:run library module as a script (terminates option list) # python -m ensurepip # 生效pip命令 Collecting setuptools Collecting pip Installing collected packages: setuptools, pip Successfully installed pip-9.0.1 setuptools-28.8.0 # python -m pip //pip生效了,根據幫助文檔使用 Usage: /usr/local/bin/python -m pip <command> [options] # pip -V //pip能正常加載python2.7的packages了 pip 9.0.1 from /usr/local/lib/python2.7/site-packages (python 2.7) # pip install urllib3 #urllib3包也能正常安裝了 Requirement already satisfied: urllib3 in /usr/local/lib/python2.7/site-packages You are using pip version 9.0.1, however version 19.2.2 is available. You should consider upgrading via the 'pip install --upgrade pip' command.
# python -m pip install --upgrade pip # pip -V pip 19.2.2 from /usr/local/lib/python2.7/site-packages/pip (python 2.7)
後來發現pip其實不必升級,因而考慮降級到默認版本,使用以下方式降級便可。url
# python -m pip install pip==9.0.1 //降級到指定版本 # pip -V //降級成功 pip 9.0.1 from /usr/local/lib/python2.7/site-packages (python 2.7)