项目背景:
环境包:cuda版本的torch、torchvision、opencv
系统环境:win10 X64,anaconda(配置好系统环境,百度一堆教程)
配置过程
1、添加源路径
在配置环境之前我们先添加其他源路径(如果不添加,会默认从官方源路径进行下载,速度很慢)
#打开终端cmd
#进入base环境
conda activate base
#中科大源
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/main/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/msys2/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/bioconda/
conda config --add channels https://mirrors.ustc.edu.cn/anaconda/cloud/menpo/
#清华源
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
conda config --set show_channel_urls yes
# 设置成功后可以用以下指令查看是否添加成功
conda config --show channels
2、创建虚拟环境
1)查看base环境下的python版本
# 进入base环境
conda activate base
# 输入python
(base) C:\Users\KYX-015>python
Python 3.7.1 (default, Dec 10 2018, 22:54:23) [MSC v.1915 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
2)这里创建一个python版本与base一直的虚拟环境
# 也不是一定得一样,这里是为了避免一些版本冲突问题引起得一些问题
#(base)表示当前所在的环境
#env_env为我们自定义的环境名
(base) C:\Users\KYX-015>conda create -n env_test python==3.7.1
The following NEW packages will be INSTALLED:
ca-certificates anaconda/pkgs/main/win-64::ca-certificates-2020.7.22-0
certifi anaconda/pkgs/main/win-64::certifi-2020.6.20-py37_0
openssl anaconda/pkgs/main/win-64::openssl-1.1.1h-he774522_0
pip anaconda/pkgs/main/win-64::pip-20.2.3-py37_0
python anaconda/pkgs/main/win-64::python-3.7.1-h8c8aaf0_6
setuptools anaconda/pkgs/main/win-64::setuptools-50.3.0-py37h9490d1a_1
sqlite anaconda/pkgs/main/win-64::sqlite-3.33.0-h2a8f88b_0
vc anaconda/pkgs/main/win-64::vc-14.1-h0510ff6_4
vs2015_runtime anaconda/pkgs/main/win-64::vs2015_runtime-14.16.27012-hf0eaf9b_3
wheel anaconda/pkgs/main/noarch::wheel-0.35.1-py_0
wincertstore anaconda/pkgs/main/win-64::wincertstore-0.2-py37_0
zlib anaconda/pkgs/main/win-64::zlib-1.2.11-h62dcd97_4
Proceed ([y]/n)? y
# 输入y开始安装python相关包
#输入下面指令进入虚拟环境
(base) C:\Users\KYX-015> conda activate env_test
(env_test) C:\Users\KYX-015>
#输入 conda list 查看虚拟环境下的包
(env_test) C:\Users\KYX-015>conda list
# packages in environment at D:\anaconda\envs\env_test:
#
# Name Version Build Channel
ca-certificates 2020.7.22 0 https://mirrors.ustc.edu.cn/anaconda/pkgs/main
certifi 2020.6.20 py37_0 https://mirrors.ustc.edu.cn/anaconda/pkgs/main
openssl 1.1.1h he774522_0 https://mirrors.ustc.edu.cn/anaconda/pkgs/main
pip 20.2.3 py37_0 https://mirrors.ustc.edu.cn/anaconda/pkgs/main
python 3.7.1 h8c8aaf0_6 https://mirrors.ustc.edu.cn/anaconda/pkgs/main
setuptools 50.3.0 py37h9490d1a_1 https://mirrors.ustc.edu.cn/anaconda/pkgs/main
sqlite 3.33.0 h2a8f88b_0 https://mirrors.ustc.edu.cn/anaconda/pkgs/main
vc 14.1 h0510ff6_4 https://mirrors.ustc.edu.cn/anaconda/pkgs/main
vs2015_runtime 14.16.27012 hf0eaf9b_3 https://mirrors.ustc.edu.cn/anaconda/pkgs/main
wheel 0.35.1 py_0 https://mirrors.ustc.edu.cn/anaconda/pkgs/main
wincertstore 0.2 py37_0 https://mirrors.ustc.edu.cn/anaconda/pkgs/main
zlib 1.2.11 h62dcd97_4 https://mirrors.ustc.edu.cn/anaconda/pkgs/main
3)选择合适版本的pytorch包
# 选择合适的pytorch版本,用conda search pytorch查找当前路径所有pytorch的版本(包括默认源路径和添加源路径)
(env_test) C:\Users\KYX-015>conda search pytorch
Loading channels: done
# Name Version Build Channel
pytorch 1.0.1 cpu_py36h39a92a0_0 anaconda/pkgs/main
pytorch 1.0.1 cpu_py36h39a92a0_0 pkgs/main
pytorch 1.0.1 cpu_py37h39a92a0_0 anaconda/pkgs/main
pytorch 1.0.1 cpu_py37h39a92a0_0 pkgs/main
pytorch 1.3.1 cpu_py36h9f948e0_0 anaconda/pkgs/main
pytorch 1.3.1 cpu_py36h9f948e0_0 pkgs/main
pytorch 1.3.1 cpu_py37h9f948e0_0 anaconda/pkgs/main
pytorch 1.3.1 cpu_py37h9f948e0_0 pkgs/main
pytorch 1.4.0 cpu_py36ha775e86_0 anaconda/pkgs/main
pytorch 1.4.0 cpu_py36ha775e86_0 pkgs/main
pytorch 1.4.0 cpu_py37ha775e86_0 anaconda/pkgs/main
pytorch 1.4.0 cpu_py37ha775e86_0 pkgs/main
pytorch 1.4.0 cpu_py38ha775e86_0 anaconda/pkgs/main
pytorch 1.4.0 cpu_py38ha775e86_0 pkgs/main
pytorch 1.5.0 cpu_py37h9f948e0_0 anaconda/pkgs/main
pytorch 1.5.0 cpu_py37h9f948e0_0 pkgs/main
pytorch 1.6.0 cpu_py37h538a6d7_0 anaconda/pkgs/main
pytorch 1.6.0 cpu_py37h538a6d7_0 pkgs/main
pytorch 1.6.0 cpu_py38h538a6d7_0 anaconda/pkgs/main
pytorch 1.6.0 cpu_py38h538a6d7_0 pkgs/main
# 显然在当前路径下没有cuda版本的pytorch,这里我们再添加一个pytorch的源路径
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
conda config --set show_channel_urls yes
#这时候你再输入conda search pytorch就会有很多个版本的pytorch了,下面列举部分版本
pytorch 1.4.0 py3.7_cuda92_cudnn7_0 anaconda/cloud/pytorch
pytorch 1.4.0 py3.8_cpu_0 anaconda/cloud/pytorch
pytorch 1.4.0 py3.8_cuda101_cudnn7_0 anaconda/cloud/pytorch
pytorch 1.4.0 py3.8_cuda92_cudnn7_0 anaconda/cloud/pytorch
pytorch 1.5.0 cpu_py37h9f948e0_0 anaconda/pkgs/main
pytorch 1.5.0 cpu_py37h9f948e0_0 pkgs/main
pytorch 1.5.0 py3.5_cpu_0 anaconda/cloud/pytorch
pytorch 1.5.0 py3.5_cuda101_cudnn7_0 anaconda/cloud/pytorch
pytorch 1.5.0 py3.5_cuda102_cudnn7_0 anaconda/cloud/pytorch
pytorch 1.5.0 py3.5_cuda92_cudnn7_0 anaconda/cloud/pytorch
pytorch 1.5.0 py3.6_cpu_0 anaconda/cloud/pytorch
pytorch 1.5.0 py3.6_cuda101_cudnn7_0 anaconda/cloud/pytorch
pytorch 1.5.0 py3.6_cuda102_cudnn7_0 anaconda/cloud/pytorch
pytorch 1.5.0 py3.6_cuda92_cudnn7_0 anaconda/cloud/pytorch
pytorch 1.5.0 py3.7_cpu_0 anaconda/cloud/pytorch
pytorch 1.5.0 py3.7_cuda101_cudnn7_0 anaconda/cloud/pytorch
pytorch 1.5.0 py3.7_cuda102_cudnn7_0 anaconda/cloud/pytorch
#可以观察,pytorch版本的不同是对应不同的python和cudatoolkit版本
#我们环境的python版本为3.7.1,同时选择cuda为9.2的pytorch
pytorch 1.4.0 py3.7_cuda92_cudnn7_0 anaconda/cloud/pytorch
4)安装cuda版本的pytorch、torchvision
# 进入pytorch官网 https://pytorch.org/
# 根据系统类型、cuda or cpu以及一些版本,生成conda install 指令
conda install pytorch torchvision cudatoolkit=10.1 -c pytorch
#这里 -c 是表示从默认源下载,速度会很慢的,所以这里要删掉,从我们指定的镜像源下载
#指定pytorch1.4.0以及cudatoolkit9.2版本,因此输入以下指令
conda install pytorch torchvision cudatoolkit=9.2 pytorch==1.4.0
#安装完成之后用conda list 查看虚拟环境下安装的包
(env_test) C:\Users\KYX-015>conda list
5)验证pytorch、numpy、torchvision
(env_test) C:\Users\KYX-015>python
Python 3.7.1 (default, Oct 28 2018, 08:39:03) [MSC v.1912 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> import numpy
>>> import torchvision
# 验证成功
6)安装opencv-python
不用conda安装,因为conda search只能找到opencv 版本
直接用pip方式安装,为了加快下载速度,-i 指定下载源
pip install opencv-python -i https://pypi.tuna.tsinghua.edu.cn/simple
相关问题总结
1、pip、conda的区别
1)一般来说如果用conda安装不了的,你可以用pip下载,pip默认从官方源下载,速度比较慢,除非你用-i指定下载源
2)相比于pip,conda安装不仅安装指定的包,比如你用conda 安装pytorch,conda安装同时还会安装pytorch所需要支持的各种包。
3)与2)一样,用conda卸载除了卸载指定的包,相应的支持包也会一起被卸载。加入我们需要降级下载一个低版本的numpy,这时候应先用pip uninstall numpy 卸载当前环境下的numpy,再用conda install numpy==1.14.0 去安装低版本的numpy
2、包之间的版本冲突
配置环境最常见的问题就是不同包之间的版本冲突问题,就比如说pytorch1.0.0仅支持低于1.15.0的numpy版本。有一种情况比如你要安装一个matlibplot,你直接用conda install matplotlib进行安装,这时候会默认给你安装匹配当前python的最新的matplotlib以及对应的numpy(为matplotlib的支持包),如果这时候numpy的版本高于1.15.0。哦豁!肯定会报错了!
1)一般我们单独安装一些包的时候,用pip会比较保险,速度慢的问题可通过指向源解决;
2)用conda单独安装一些包的时候,注意一些一些支持包的更新覆盖。比如我们现在安装一个包matplotlib!
#如果在当前的源路径下找不到适合版本的matlibplot,直接进入清华镜像源网站https://mirrors.tuna.tsinghua.edu.cn/查看matlibplot版本,用 -i指定源地址
#一般都是可以找到的
#这里我们的numpy版本为1.19.1,python版本为3.7.1--->所以选择matplotlib==3.1.2
matplotlib 3.1.2 py37h64f37c6_0 anaconda/pkgs/main
matplotlib 3.1.2 py37h64f37c6_0 pkgs/main
#输入conda install matplotlib==3.1.2 显示如下
The following NEW packages will be INSTALLED:
cycler anaconda/pkgs/main/win-64::cycler-0.10.0-py37_0
icu anaconda/pkgs/main/win-64::icu-58.2-ha925a31_3
kiwisolver anaconda/pkgs/main/win-64::kiwisolver-1.2.0-py37h74a9793_0
matplotlib anaconda/pkgs/main/win-64::matplotlib-3.1.2-py37_1
matplotlib-base anaconda/pkgs/main/win-64::matplotlib-base-3.1.2-py37h64f37c6_1
pyparsing anaconda/pkgs/main/noarch::pyparsing-2.4.7-py_0
pyqt anaconda/pkgs/main/win-64::pyqt-5.9.2-py37h6538335_2
python-dateutil anaconda/pkgs/main/noarch::python-dateutil-2.8.1-py_0
qt anaconda/pkgs/main/win-64::qt-5.9.7-vc14h73c81de_0
sip anaconda/pkgs/main/win-64::sip-4.19.8-py37h6538335_0
sqlite anaconda/pkgs/main/win-64::sqlite-3.33.0-h2a8f88b_0
tornado anaconda/pkgs/main/win-64::tornado-6.0.4-py37he774522_1
The following packages will be UPDATED:
openssl anaconda/pkgs/free::openssl-1.0.1k-1 --> anaconda/pkgs/main::openssl-1.1.1h-he774522_0
Proceed ([y]/n)?
#这时候要注意观察到哪一些包会被更新
#这里openss1被更新升级了,其他关键的支持包numpy没有被改变,我们输入y继续安装即可
#但如果numpy被要求更新,我们应该选择适合版本的matplotlib去匹配当前的numpy
简单进行验证
(env_test) C:\Users\KYX-015>python
Python 3.7.1 (default, Oct 28 2018, 08:39:03) [MSC v.1912 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib
>>> quit()