原Python計算環境是Python3.6版本,以及基於3.6的Tensoflow1.13,由於Python3.6已經沒有支持,因此,現升級到3.8。
1.首先安裝依賴包,安裝相應的編譯工具
由於是升級,CentOS環境依賴已經具備,此步驟跳過。
[[email protected] bin]# yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-de
2.下載安裝包
在當前用戶下,把安裝包下載到software文件夾下,下載有兩種方式:
一是直接從官方網站手工下載:
https://www.python.org/ftp/python/3.8.13/Python-3.8.13.tgz
二是命令獲取:
[[email protected] python]# cd software
[[email protected] software]$ wget http://www.python.org/ftp/python/3.8.13/Python-3.8.13.tgz
3.解壓
[[email protected] software]# tar zxvf Python-3.8.13.tgz
4.備份修改原python3(3.6版本)
[[email protected] software]# cd /usr/local
[[email protected] local]# mv python3 python3.6
5.創建編譯安裝目錄
[[email protected] local]# mkdir /usr/local/python3
[[email protected] local]# ls
bin etc games include lib lib64 libexec python3 python3.6 sbin share src
6.安裝
指定安裝的路徑
[[email protected] local]# cd /home/python/software/Python-3.8.13
[[email protected] Python-3.8.13]# ./configure --prefix=/usr/local/python3
編譯
[[email protected] Python-3.8.13]# make
報錯:
/home/python/software/Python-3.8.13/Modules/_ctypes/_ctypes.c:107:17: 致命錯誤:ffi.h:沒有那個文件或目錄
編譯中斷。
解決方案:
[[email protected] Python-3.8.13]# yum install libffi-devel
安裝
[[email protected] Python-3.8.13]# make install
7.創建軟鏈接
由於是升級,原先已經定義過,此步可以跳過。
[[email protected] bin]# ln -s /usr/local/python3/bin/python3 /usr/bin/python3
[[email protected] bin]# ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
需要核實驗證原定義是否有效。
[[email protected] bin]# ls -l /usr/bin/python3
/usr/bin/python3 -> /usr/local/python3/bin/python3
[[email protected] bin]# ls -l /usr/bin/pip3
/usr/bin/pip3 -> /usr/local/python3/bin/pip3
8.驗證是否成功
python3 -V
pip3 -V
9.配置環境變量【可選】
[[email protected] bin]# vi ~/.bash_profile
編輯配置文件
export PYTHON_HOME=/usr/local/python3
export PATH=$PYTHON_HOME/bin:$PATH
執行配置文件,生效
[[email protected] bin]# source ~/.bash_profile
[[email protected] bin]# echo $PYTHON_HOME
/usr/local/python3
安裝tensorflow2.6,依賴numpy1.19.5版本。
# 安裝numpy1.19.5
[[email protected] bin]# pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple numpy==1.19.5
# 安裝圖像依賴imageio2.9.0
[[email protected] bin]# pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple imageio==2.9.0
[[email protected] bin]# pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple scikit-image
[[email protected] bin]# pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple opencv-python
[[email protected] bin]# pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple matplotlib
[[email protected] bin]# pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple tensorflow==2.6.1
# 數值計算包
[[email protected] bin]# pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple scipy
[[email protected] bin]# pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple pandas
[[email protected] bin]# pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple scikit-learn
[[email protected] bin]# pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple seaborn
# 貝葉斯優化包
[[email protected] bin]# pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple bayesian-optimization
XGBoost新版本需要依賴cmake3.15以上版本,以及gcc5.0以上版本,升級cmake和gcc過程如下所示,升級過程直接跳過。
[[email protected] Downloads]# wget https://cmake.org/files/v3.17/cmake-3.17.1.tar.gz
[[email protected] Downloads]# tar -zxvf cmake-3.17.1.tar.gz
[[email protected] Downloads]# cd cmake-3.17.1
[[email protected] cmake-3.17.1]# ./bootstrap --prefix=/usr
CMake has bootstrapped. Now run gmake.
[[email protected] cmake-3.17.1]# gmake
[[email protected] cmake-3.17.1]# gmake install
升級gcc到5.x
如果已經安裝,可以略過:
[[email protected] Downloads]# yum install bzip2
[[email protected] Downloads]# wget http://mirrors.ustc.edu.cn/gnu/gcc/gcc-5.4.0/gcc-5.4.0.tar.bz2
[[email protected] Downloads]# tar -jxvf gcc-5.4.0.tar.bz2
[[email protected] gcc-5.4.0]# cd gcc-5.4.0
[[email protected] gcc-5.4.0]# ./contrib/download_prerequisites
[[email protected] bin]# /home/python/Downloads/gcc-5.4.0/configure --enable-checking=release --enable-languages=c,c++ --disable-multilib
[[email protected] bin]# make
[[email protected] bin]# make install
保留一下4.8.5的gcc,後續需要回退的時候,可以方便很多
[[email protected] bin]# mv gcc gcc-4.85
[[email protected] bin]# mv g++ g++-4.85
[[email protected] bin]# mv /usr/lib64/libstdc++.so.6 /usr/lib64/libstdc++.so.6-4.85
引用處理改為新版本
[[email protected] bin]# ln -s /usr/local/bin/gcc /usr/bin/gcc
[[email protected] bin]# ln -s /usr/local/bin/g++ /usr/bin/g++
[[email protected] bin]# ln /usr/local/lib64/libstdc++.so.6 /usr/lib64/libstdc++.so.6
安裝xgboost。
[[email protected] bin]# pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple xgboost
[[email protected] bin]# pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple flask
[[email protected] bin]# pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple tornado
[[email protected] bin]# pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple streamlit==1.9.1
streamlit 1.10及以上版本與tensorflow2沖突:
tensorflow 2.6.1 requires typing-extensions~=3.7.4, but you have typing-extensions 4.2.0 which is incompatible.
[[email protected] bin]# pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple pymongo
[[email protected] bin]# pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple clickhouse_driver
卸載的時候直接把該⽬錄刪了即可:
rm -rf /usr/local/python3.6
對於Eclispe插件PyDev的配置如下:
PyDev/Interpreters/Python Interpreter中,通過“Browse for python/pypy.exe”按鈕替換原來的“/usr/local/python3/bin/python3.6”為“/usr/local/python3/bin/python3.8”。
Python升級比較容易,很多環境依賴已經具備,只要重新安裝新版本Python就可以,但是算法計算包升級要謹慎。
參考:
[1].肖永威. 基於CentOS7安裝Python3計算環境與PyDev(Eclipse)開發工具日志(2021年). CSDN博客. 2021.04
[2]. 知識天罡茹薇00. linux安裝和卸載python3 . 百度文庫
[3]. weixin_39892615. python沒有那個文件或目錄_致命錯誤:python.h:沒有那個文件或目錄. CSDN博客. 2020.11