文章目录
介绍语
你好! 这是我使用anaconda进行Python虚拟环境配置的一些命令。
1 自动记录程序运行时间
(参考https://blog.csdn.net/weixin_39679379/article/details/128015001)
import time
start = time.clock()
程序
end = time.clock()
print("运行耗时", end-start)
Python3.8不再支持time.clock(),如果使用Python3.8:
import time
start = time.perf_counter()
程序
end = time.perf_counter()
print("运行耗时", end-start)
2 虚拟环境安装opencv-python(cv2)
(参考https://blog.csdn.net/qq_44111805/article/details/128287948)
去下载镜像文件到本地,然后进入到下载目录,pip安装
3 查看虚拟环境下的cuda版本
nvcc --version
import torch
print(torch.__version__)#pytorch的版本
print(torch.version.cuda)#cuda版本
print(torch.backends.cudnn.version())
4 修改包的管理镜像为国内源
Anaconda安装成功之后,我们需要修改其包管理镜像为国内源Tsinghua Open Source Mirror
简单来说就是在cmd中分别运行这两个命令就好了。
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --set show_channel_urls yes
5 给虚拟环境指定Python版本
如果我们需要添加2.7版本的Python,可以进行如下操作:
1.
conda create -n py27 python=2.7 #其中py27是新添加环境的名字,可以自定义修改
(之后可以通过activate py27
和deactivate py27
命令激活、退出该环境)
- 在激活新环境后,我们所做的操作便都是在新环境下的,包括pip命令来安装第三方库。
试一下pip安装lxml包(其他包类似语句):pip install lxml
或conda install lxml
通过conda info -e
命令查看已创建的虚拟环境们
通过conda remove -n 虚拟环境名 --all
来删除指定的环境(如果不添–all参数,而是指明某个库名,则是删除该库)。
- 测试pytorch
(1)进入python:python
(2)测试pytorch:
import torch
torch.cuda.is_available() #验证gpu版本的torch是否成功
6 在虚拟环境中安装cuda
- 首先要看好cudnn与cudatoolkit与pytorch包的版本对应;
- 装cudatoolkit:
conda install cudatoolkit=11.3 -c https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/win-64/
- 装对应cudnn:(不是必须要装cudnn,要看代码的运行要求)
conda install cudnn=8.0.5 -c https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/win-64/
- 就可以装cuda对应版本的torch包:
网址:https://pytorch.org/get-started/previous-versions/
7 打开jupyter notebook
不要从anaconda navigate里面打开!
不要用anaconda下载后自带的jupyter notebook,要在anaconda prompt的虚拟环境下重新安装的jupyter notebook才可以正常import(反正我的是这样)
解决方法:(第一次打开需要安装jupyter包)
- 打开anaconda prompt
- 激活虚拟环境
- 安装jupyter notebook:在anaconda prompt中输入
conda install jupyter
- 再在anaconda prompt中去打开jupyter notebook,输入
jupyter notebook
平时打开jupyter notebook步骤:
- 打开anaconda prompt
- 激活对应虚拟环境
- 打开jupyter notebook:输入
jupyter notebook
8 查看pytorch版本
- 进入python:虚拟环境下输入
python
- 输入:
import torch
print(torch.__version__)
9 pytorch官网
https://pytorch.org/get-started/locally/
安装pytorch看虚拟环境下安装的cuda版本或直接安装cpu版本
10 pyg包官网
https://pytorch-geometric.readthedocs.io/en/latest/install/installation.html#
11 查看目前安装的cuda们
- 进入目录:C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA
可以看到已经安装的cuda版本:10.2,11.1,11.3 - 更换cuda“应该”去系统环境配置里面修改优先级
12 cudnn网站
https://developer.nvidia.com/rdp/cudnn-archive
我给cuda10.2配的cudnn版本是7.6.5