实际操作:(1)打开anaconda prompt,创建虚拟环境(如出现安装虚拟环境失败,找到.condarc文件删除)
conda create --name XXXXX python=XXXXX
(2)激活该虚拟环境
conda activate XXXXX
安装包的完全环境隔离
conda install pip
(3)查看并设置镜像源
常用网址
阿里云 http://mirrors.aliyun.com/pypi/simple/
豆瓣 http://pypi.douban.com/simple
中国科学院 http://pypi.mirrors.opencas.cn/simple/
清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/
中国科学技术大学 [http://pypi.mirrors.ustc.edu.cn/simple/
pip方法
查看镜像源
pip config list
永久添加新镜像源和主机
pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/
pip config set global.trusted-host https://mirrors.aliyun.com/pypi/simple/
移除镜像源和主机
pip config set global.index-url xxxxx
pip config unset global.trusted-host
conda方法
查看镜像源
conda config --show channels
添加镜像源
conda config --add channels https://mirrors.aliyun.com/pypi/simple/
conda config --add channels https://mirrors.aliyun.com/pypi/anaconda/cloud/pytorch/
conda config --add channels https://mirrors.aliyun.com/pypi/anaconda/pkgs/main/
conda config --add channels https://mirrors.aliyun.com/pypi/anaconda/pkgs/free/
移除镜像源
conda config --remove-key channels
移除指定源
conda config --remove-key channels xxx(网站)
验证镜像源
conda config --show-sources
(4)下载项目中的包
定位到项目文件夹
cd “ 文件目录”
批量下载
pip install -r requirements.txt
单个下载
pip install XXXXXX.X.X -i https://mirrors.aliyun.com/pypi/simple/
(5)运行项目
pip install -e .
具体细节:
(1)下载anaconda之后,首先打开anaconda prompt,输入
conda --version获取当前anaconda的版本。
有可能会出现:
首先要清理所有的包:conda clean --packages –tarballs。
可以Win+R,输入cmd,进入命令行后再输入conda list验证
加入镜像下载地址:
(在创造新环境时,这些源会被隔离,因此需要用 pip config set global.index-url https://mirrors.aliyun.com/pypi/simple来代替,否则下载速度会很慢)
conda config --add channels https://mirrors.aliyun.com/anaconda/pkgs/free/
conda config --add channels https://mirrors.aliyun.com/anaconda/pkgs/main/
启动设置好的镜像源:
conda config --set show_channel_urls yes
查看镜像源
conda config --show-sources
设置灵活的镜像源来源
conda config --set channel_priority flexible
删除镜像源:
conda config --remove channels conda config --add channels[空格]网站名称
切换默认源
conda config –remove-key channels
将pip的下载源永久更改为某个镜像站
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple/
(2)设置虚拟环境
conda create --name Quantitative(名字可以替换)
激活设置的conda环境(每次都可以用这个)
conda activate Quantitative
退出环境
Deactivate
删除环境
conda remove -n Quantitative –all
当前环境
conda info -e
特别提醒:设置好一个虚拟环境以后,如果直接使用pip install XXX的话,会在base环境也安装
因此需要在该虚拟环境中使用 conda install pip,实现pip安装的环境隔离,非常重要!!
(3)安装torch和torchversion,注意和python的版本要一致。
conda install pytorch1.12.1
conda install torchvision==0.13.1
(4)检查库是否安装成功
import 库的名称
库的名称.version.version
(5)安装pytorch
使用:import torch
import torchvision
未出现问题即安装成功。或者进入anaconda navigator查看。
查看pytorch版本: 查看torchvision版本
import torch import torchvision
torch.version torchvision.version
或者:激活pytorch环境
conda activate pytorch
• 问题:Solving environment: failed with initial frozen solve. Retrying with flexible solve
未更新canda环境。
解决:
- conda update -n base conda
- conda update --all
注:jupyter中虚拟环境的设置
(1)首先需要注意anaconda自带jupyter,因此必须在base环境下进行jupyter指令的输入
(2)在base环境下查看jupyter所有的kernel:jupyter kernelspec list;删除指定的kernel:jupyter kernelspec remove ‘kernelname’
(3)在base环境下安装核:conda install jupyter ipykernel
(4)进入虚拟环境安装核:conda install ipykernel;在虚拟环境安装jupyter(未确定必要性,在pycharm中使用虚拟环境需要)
(5)在base环境下将虚拟环境写入jupyter的kernel中:
python -m ipykernel install --user --name 环境名称
(6)激活jupyter:
#激活jupyter Notebook
conda activate base
jupyter notebook
#激活jupyter lab
conda activate base
jupyter lab
总结:要在不同的虚拟环境下使用jupyter时,首先要确认jupyter的安装路径在哪一个环境,定位到此环境,一般情况下base环境会有kernel(需不需要安装ipykernel暂且未知,推荐安装),进入虚拟环境安装ipykernel和jupyter,切换到base环境将虚拟环境的kernel写入jupyter中,激活相应的jupyter软件。
具体在该博客中出现
(2)注:如果出现安装虚拟环境失败,找到.condarc文件删除,再重新安装虚拟环境即可。