我使用的Linux系统版本为CentOS 6.10(查看命令:cat /etc/issue), 该系统自带的python为2.6版本,该版本已经十分老化,很多基于python2平台运行的程序已经无法继续支持。因此需要安装python2.7,安装步骤祥见”写Scala的老刘“的博客link,需要申明的是,由于CentOS的很多底层程序是关联python2.6版本的,因此一定不能将其卸载。卸载后会引起yum安装命令失效等一系列严重的问题,关于该问题的修复办法我会在后续发布。
当然,目前最主流的python平台还是3系列。除非用到一些基于python2平台的程序,否则是没有必要更新python2.7的。至于python3,推荐直接使用Anaconda集成版本来安装link,安装步骤祥见“CoderYYN”的博客link.
特别注意,如果同时安装了python2.7和python3,则每次在调用相应的python版本时,需要更改PYTHONPATH的环境变量。以我的模块安装路径为例:
# 临时更改
## 使用python2.7版本
export PYTHONPATH=/usr/local/python27/lib/python2.7/site-packages
## 使用python3版本
export PYTHON3PATH=/usr/local/Anaconda3/lib/python3.7/site-packages
# 如果将python3版本设置为永久默认
## 打开环境变量
vim /etc/profile
## 在最后一行添加
export PYTHON3PATH=$PATH:/usr/local/Anaconda3/lib/python3.7/site-packages
## 使环境变量生效
source /etc/profile
该问题是由于python2.7的默认路径里面没有包含进去模块所在的路径所引起的,解决办法:
# 首先启动python2.7
python2.7
# 查看python2.7的默认路径
import sys
print(sys.path)
# 退出
exit()
# 搜索安装模块的路径
find /usr -name 'site-packages' -print
# 对比以上显示的路径,从中找到属于python2.7的安装模块路径
# 比如我的是 “/usr/local/python27/lib/python2.7/site-packages”
# 将该路径添加到环境变量
export PYTHONPATH=/usr/local/python27/lib/python2.7/site-packages
# 完成
该问题是由于yum命令是依赖python2.6来执行的,因此安装的MySQL-python版本为1.2.3版本,而python2.7需要1.2.5版本来兼容。解决办法如下:
# 下载MySQL-python-1.2.5
wget -c https://files.pythonhosted.org/packages/a5/e9/51b544da85a36a68debe7a7091f068d802fc515a3a202652828c73453cad/MySQL-python-1.2.5.zip
# 解压缩并安装
unzip MySQL-python-1.2.5.zip
cd MySQL-python-1.2.5
python setup.py build
python setup.py install
# 完成
可以利用清华大学的PyPi镜像源来替代源网站,该网站镜像每5分钟同步一次,具体操作指令如下:
# 临时使用
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple some-package
# 改为默认(建议)
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pip -U
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
另外,如果在下载安装过程中,由于网速不稳定出现**raise ReadTimeoutError(self._pool, None, ‘Read timed out.’)**的情况,可以在pip命令后增加延长time out的时间:
pip --default-timeout=100 install "PackageName"
linux自带python被误删之后,会影响很多基层程序的使用。
# 强制删除已安装程序及其关联
rpm -qa|grep python|xargs rpm -ev --allmatches --nodeps
# 删除所有残余文件 #xargs,允许你对输出执行其他某些命令
whereis python|xargs rm -frv
# 卸载yum
rpm -qa|grep yum|xargs rpm -ev --allmatches --nodeps
rm -rf /etc/yum.repos.d/*
whereis yum|xargs rm -frv
# 验证是否删除干净( 验证删除,返回应该是没有结果的)
whereis python
whereis yum
# 新建dl.bash文件,并将以下内容拷贝进去
wget http://mirrors.ustc.edu.cn/centos/6/os/x86_64/Packages/python-2.6.6-66.el6_8.x86_64.rpm
wget http://mirrors.ustc.edu.cn/centos/6/os/x86_64/Packages/python-devel-2.6.6-66.el6_8.x86_64.rpm
wget http://mirrors.ustc.edu.cn/centos/6/os/x86_64/Packages/python-libs-2.6.6-66.el6_8.x86_64.rpm
wget http://mirrors.ustc.edu.cn/centos/6/os/x86_64/Packages/python-pycurl-19.0-9.el6.x86_64.rpm
wget http://mirrors.ustc.edu.cn/centos/6/os/x86_64/Packages/python-setuptools-0.6.10-4.el6_9.noarch.rpm
wget http://mirrors.ustc.edu.cn/centos/6/os/x86_64/Packages/python-urlgrabber-3.9.1-11.el6.noarch.rpm
wget http://mirrors.ustc.edu.cn/centos/6/os/x86_64/Packages/python-iniparse-0.3.1-2.1.el6.noarch.rpm
wget http://mirrors.ustc.edu.cn/centos/6/os/x86_64/Packages/rpm-python-4.8.0-59.el6.x86_64.rpm
wget http://mirrors.ustc.edu.cn/centos/6/os/x86_64/Packages/yum-3.2.29-81.el6.centos.noarch.rpm
wget http://mirrors.ustc.edu.cn/centos/6/os/x86_64/Packages/yum-metadata-parser-1.1.2-16.el6.x86_64.rpm
wget http://mirrors.ustc.edu.cn/centos/6/os/x86_64/Packages/yum-utils-1.1.30-41.el6.noarch.rpm
wget http://mirrors.ustc.edu.cn/centos/6/os/x86_64/Packages/yum-plugin-fastestmirror-1.1.30-41.el6.noarch.rpm
wget http://mirrors.ustc.edu.cn/centos/6/os/x86_64/Packages/yum-plugin-protectbase-1.1.30-41.el6.noarch.rpm
wget http://mirrors.ustc.edu.cn/centos/6/os/x86_64/Packages/yum-plugin-aliases-1.1.30-41.el6.noarch.rpm
# 运行dl.bash文件,进行下载
bash dl.bash
rpm -Uvh --replacepkgs python*.rpm --nodeps --force
rpm -Uvh --replacepkgs rpm*.rpm --nodeps --force
rpm -Uvh --replacepkgs yum*.rpm --nodeps --force
驱动下载网址:link
CUDA下载网址:link
cuDNN下载网址:link
驱动及CUDA均下载runfile版本.
# 旧驱动卸载
cd /usr/bin
nvidia-uninstall
# 新驱动安装
init 3 # 关闭X manager
sh NVIDIA-Linux-x86_64-440.64.00.run
init 5
Install Nvidia's 32-bit compatibility libraries? 选择 No 继续。
Would you like to run the nvidia-xconfigutility to automatically update your x configuration so that the NVIDIA x driver will be used when you restart x? Any pre-existing x confile will be backed up. 选择 Yes 继续
# 卸载旧版CUDA
cd /usr/local/cuda-6.5/bin
./unistall_cuda_6.5.pl
# 安装新版(copy from [https://www.cnblogs.com/shenggang/p/12133220.html])
sh cuda_9.0.176_384.81_linux.run
# 然后将文件翻到最后,进行一些选项
Do you accept the previously read EULA?
accept/decline/quit: accept
# 非常关键,我们已经在之前安装了高版本的驱动,这个千万别装。
Install NVIDIA Accelerated Graphics Driver for Linux-x86_64 384.81?
(y)es/(n)o/(q)uit: n
Install the CUDA 9.0 Toolkit?
(y)es/(n)o/(q)uit: y
Enter Toolkit Location
[ default is /usr/local/cuda-9.0 ]:
Do you want to install a symbolic link at /usr/local/cuda?
(y)es/(n)o/(q)uit: y
Install the CUDA 9.0 Samples?
(y)es/(n)o/(q)uit: y
Enter CUDA Samples Location
[ default is /root ]: /usr/local/cuda-10.2/
# 配置环境变量
vim /etc/profile
export PATH=${PATH}:/usr/local/cuda/bin
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/usr/local/cuda/lib64
source /etc/profile
# 验证
nvcc -V
# 解压cuDNN的包 (注意这里解压后的文件目录叫做“cuda”)
tar -xvf cudnn-9.0-linux-x64-v7.tgz
# 将解压后的文件复制到cuda的安装后的目录中:
cp cuda/include/cudnn.h /usr/local/cuda-10.2/include
cp cuda/lib64/libcudnn* /usr/local/cuda-9.0/lib64
# 将上面复制后的文件全部添加“a+r”权限:
chmod a+r /usr/local/cuda-9.0/include/cudnn.h /usr/local/cuda-9.0/lib64/libcudnn*
在centos系统中opencv_python模块由于缺少系统依赖难以直接安装,解决方法如下:
# 配置yum下载器
yum install yum-utils
# 下载系统依赖rpm包
yumdownloader libICE
yumdownloader libSM
yumdownloader libX11
yumdownloader libX11
yumdownloader libXau
yumdownloader libxcb
yumdownloader libXrender
yumdownloader libXext
# 进入python,使用以下命令测试
import tensorflow as tf
tf.test.gpu_device_name()
如果出现以下情况:
2020-05-26 13:41:11.299037: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Could not dlopen library 'libcudart.so.10.0'; dlerror: libcudart.so.10.0: cannot open shared object file: No such file or directory
2020-05-26 13:41:11.299176: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Could not dlopen library 'libcublas.so.10.0'; dlerror: libcublas.so.10.0: cannot open shared object file: No such file or directory
2020-05-26 13:41:11.299257: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Could not dlopen library 'libcufft.so.10.0'; dlerror: libcufft.so.10.0: cannot open shared object file: No such file or directory
2020-05-26 13:41:11.299336: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Could not dlopen library 'libcurand.so.10.0'; dlerror: libcurand.so.10.0: cannot open shared object file: No such file or directory
2020-05-26 13:41:11.299413: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Could not dlopen library 'libcusolver.so.10.0'; dlerror: libcusolver.so.10.0: cannot open shared object file: No such file or directory
2020-05-26 13:41:11.299490: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Could not dlopen library 'libcusparse.so.10.0'; dlerror: libcusparse.so.10.0: cannot open shared object file: No such file or directory
需要未配置 LD_LIBRARY_PATH(注意路径中的CUDA版本):
# 进入配置文件
vim ~/.bashrc
# 在文末添加命令
~~export LD_LIBRARY_PATH="/usr/local/cuda-10.2/lib64:$LD_LIBRARY_PATH"~~
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/cuda-10.2/lib64:/usr/local/cuda-10.2/extras/CUPTI/lib64
export CUDA_HOME=/usr/local/cuda-10.2
# 配置生效
source ~/.bashrc
tensorflow-gpu-2.2.0版本指定NVIDIA驱动为10.1版本。若安装的为10.2版本,则需要增加两个软连接:
ln -s libcudart.so.10.2.89 libcudart.so.10.1
ln -s libcupti.so.10.2.75 libcupti.so.10.1
ln -s libcudnn.so.6 libcudnn.so.7