CentOS下升级python版本
源碼安裝python
-
安裝python源碼所依賴的工具及依賴的庫(kù)
yum install -y make gcc gcc-c++ yum install -y bzip2 bzip2-devel yum install zlib-devel openssl openssl-devel -y yum install -y make xz -
下載安裝python源碼
從官方網(wǎng)站或者華為鏡像源下載所有需的源碼包,此處以Python-2.7.10.tar.xz為例.
wget https://www.python.org/ftp/python/2.7.10/Python-2.7.10.tar.xz yum -y install make xz xz -d Python-2.7.10.tar.xz tar xvf Python-2.7.10.tar cd Python-2.7.10 ./configure --enable-shared make make installmake install can overwrite or masquerade the python3 binary. make altinstall is therefore recommended instead of make install since it only installs exec_prefix/bin/pythonversion.
-
通過下面命令可以查看安裝包的路徑
$ python -c "from distutils.sysconfig import get_python_lib; print (get_python_lib())" /usr/lib/python2.7/site-packages $ python -c "import os; print os.__file__" /usr/lib64/python2.7/os.pyc $ python -c "import sys; print sys.executable" /usr/bin/python -
其他配置
-
解決python運(yùn)行時(shí)找不到動(dòng)態(tài)庫(kù)的問題
echo "/usr/local/lib" >> /etc/ld.so.conf # 將/usr/local/lib添加到/etc/ld.so.conf文件末尾 ldconfig -
工具yum使用的原版的python,在這里需要處理。
vi /usr/bin/yum將第一行的#!/usr/bin/python修改為系統(tǒng)原有的python版本地址#!/usr/bin/python2.6
-
-
python升級(jí)腳本
#!/bin/bashFILE_DIR=$(cd "$(dirname "$0")"; pwd)function upgrade_python {# 安裝編譯python源碼必須的工具yum -y install wget xz yum -y install gcc gcc-c++yum -y install make automake autoconfyum -y install bzip2 bzip2-devel bzip2-libs zlib-develyum -y install openssl openssl-develyum -y install bind-utils# 下載python源碼包[ -e Python-2.7.11.tar.xz ] || wget https://www.python.org/ftp/python/2.7.11/Python-2.7.11.tar.xzxz -k -d Python-2.7.11.tar.xztar xvf Python-2.7.11.tarcd Python-2.7.11./configure --enable-sharedmakemake installcd "${FILE_DIR}"# 解決Python運(yùn)行時(shí)找不到動(dòng)態(tài)庫(kù)的問題echo "/usr/local/lib" > /etc/ld.so.conf.d/python2.7.confldconfig# yum工具默認(rèn)依賴python2.6,需要修改sed -i '1s/python$/python2.6/' /usr/bin/yum# 備份/usr/bin/python,連接到新安裝的python版本上mv /usr/bin/python{,_bk}ln -s /usr/local/bin/python /usr/bin/python# 安裝pip工具wget https://bootstrap.pypa.io/get-pip.py -O - | python }upgrade_python -
python -c "import sysconfig; print(sysconfig.get_config_vars(\"CONFIG_ARGS\"))"可以查看python的配置參數(shù)
Sat Mar 6 17:48:27 CST 2021 CentOS7 Python3.8.8
yum install gcc gcc-c++ make pkgconfig yum install libffi-devel zlib-devel xz-devel bzip2-devel readline-devel sqlite-devel ncurses-devel gdbm-devel openssl-devel expat-devel libtirpc-devel libuuid-devel CXX=/usr/bin/g++ ./configure --prefix=/usr/local/python388 --with-ssl-default-suites=openssl make -j 4 make altinstall ls /usr/local/python388/bin/ # 2to3-3.8 easy_install-3.8 idle3.8 pip3.8 pydoc3.8 python3.8 python3.8-config工具包安裝及使用
打包工具setuptools
- 功能:Easily download, build, install, upgrade, and uninstall Python packages
- 具體安裝方法請(qǐng)自行參考:https://pypi.python.org/pypi/setuptools#downloads
- 官網(wǎng)倡導(dǎo)的安裝方法:wget https://bootstrap.pypa.io/ez_setup.py -O - | python
此包給用戶使用的easy_install工具,安裝需要的包簡(jiǎn)單易用,開發(fā)者發(fā)布包的所需指導(dǎo)。
- The EasyInstall user’s guide and reference manual
- The setuptools Developer’s Guide
- The pkg_resources API reference
- The Internal Structure of Python Eggs
包安裝工具pip
- 功能:The PyPA recommended tool for installing Python packages
- wget --no-check-certificate https://bootstrap.pypa.io/get-pip.py -O - | python
- pip主頁(yè)地址:https://pypi.python.org/pypi/pip#downloads
- 源碼安裝wget https://pypi.python.org/packages/source/p/pip/pip-7.1.2.tar.gz#md5=3823d2343d9f3aaab21cf9c917710196tar zxvf pip-7.1.2.tar.gz cd pip-7.1.2 python setup.py install
安裝后的工具path:PythonXX/Scripts,在window下有必要將此路徑加入環(huán)境變量path中
使用pip:
- pip --help 查看pip的幫助文檔
- pip list 查看已安裝的packages
- pip install xlrd 安裝xlrd,用于處理Excel的packages
其他應(yīng)用請(qǐng)參照pip幫助
Python自動(dòng)文檔工具pydoc
首先認(rèn)識(shí)一條比較重要的命令
python -m mod 作為腳本運(yùn)行mod庫(kù),if name == 'main’是作為腳本運(yùn)行的部分.
- python -m SimpleHTTPServer 6666 #簡(jiǎn)單一個(gè)命令搭建目錄服務(wù)器
- 查看math庫(kù)手冊(cè) python -m pydoc math
- 查看函數(shù)手冊(cè) python -m pydoc math.asin
- 開啟本地手冊(cè)服務(wù)器,可以再網(wǎng)頁(yè)上查看所有的庫(kù)函數(shù)手冊(cè),感覺十分強(qiáng)大 python -m pydoc -p port
python2腳本轉(zhuǎn)化為python3腳本的工具2to3
python是中十分強(qiáng)大的語(yǔ)言,但它的高速發(fā)展,也給廣大開發(fā)這帶來許多困惑,python3并不兼容python2
Python3x/Tools/Scripts/2to3.py
C:/Python34/Tools/Scripts/2to3.py -w test.py
#test.py def greet(name):print("Hello, {0}!".format(name)) print("What's your name?") name = input() greet(name)這里可以看到2to3后的變化
pirnt函數(shù)函數(shù)調(diào)用的方式使用,raw_input替換成了input
詳細(xì)轉(zhuǎn)換請(qǐng)參照python3的手冊(cè)以及http://woodpecker.org.cn/diveintopython3/porting-code-to-python-3-with-2to3.html
參考
- http://www.aiplaypc.com/31.html
- Python Developer’s Guide?
- [https://devguide.python.org/setup/#build-dependencies](Install dependencies)
- Common build problems
總結(jié)
以上是生活随笔為你收集整理的CentOS下升级python版本的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 管理实践-教练技术的应用
- 下一篇: k-means均值向量