炼丹环境配置
# 炼丹环境配置
# 1.安装Anaconda
- 下载Anaconda安装包
wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda3-2020.07-Linux-x86_64.sh
注意:如果wget报错,说明目标网站屏蔽掉了wget的user agent。需要使用-U指名模拟浏览器的user agent。浏览器打开F12查看请求头:

添加请求头,注意linux不支持括号,需要加入转义字符'\'
wget -U Edg/114.0.1823.82 https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda3-2020.07-Linux-x86_64.sh
- 安装Anaconda(或者直接用服务器上面的镜像)
bash Anaconda3-2020.07-Linux-x86_64.sh
【1】回车:查看注册信息
【2】按q:跳过阅读
【3】yes
【4】回车:默认在/root/下创建并安装anaconda3
【5】yes或者no都可以:yes则第一次激活启动环境之前,需要执行conda init,后面再激活环境就不需要了。
- 配置环境变量
将Anaconda添加到用户变量中:
vim ~/.bashrc
在最底行添加以下内容:
#放置与系统的python冲突,使用其它变量名fortunePython指定base环境下的python解释器
alias fortunePython='/root/anaconda3/bin/python'
#这里写anaconda的安装路径
export PATH="/root/anaconda3/bin:$PATH"
2
3
4
使配置生效:
source ~/.bashrc
- 检验是否安装完成
anaconda -V
conda --version
fortunePython
2
3
# 2.conda配置清华源
恢复默认源头:
conda config --show channels
换成清华源:
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
#pytorch相关的源
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
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/pytorch/
#设置搜索时显示通道地址
conda config --set show_channel_urls yes
2
3
4
5
6
7
8
查看安装源和信息:
conda config --show-sources
conda info
2
另外切换数据源时,如果出现下载不了的情况,需要切换默认源再修改另一个可以使用的conda源
#切换默认源
conda config --remove-key channels
#更新数据源
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --set show_channel_urls yes
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
2
3
4
5
6
7
如果不管用,直接修改.condarc文件:
channels:
- defaults
show_channel_urls: true
default_channels:
- http://mirrors.aliyun.com/anaconda/pkgs/main
- http://mirrors.aliyun.com/anaconda/pkgs/r
- http://mirrors.aliyun.com/anaconda/pkgs/msys2
custom_channels:
conda-forge: http://mirrors.aliyun.com/anaconda/cloud
msys2: http://mirrors.aliyun.com/anaconda/cloud
bioconda: http://mirrors.aliyun.com/anaconda/cloud
menpo: http://mirrors.aliyun.com/anaconda/cloud
pytorch: http://mirrors.aliyun.com/anaconda/cloud
simpleitk: http://mirrors.aliyun.com/anaconda/cloud
2
3
4
5
6
7
8
9
10
11
12
13
14
# 3.conda环境的创建
conda环境相关命令如下:
#创建虚拟环境
conda create -n your_env_name python=X.X(3.8、3.9等)
#激活虚拟环境
conda activate your_env_name(虚拟环境名称)
#退出虚拟环境
conda deactivate
#删除虚拟环境
conda remove -n your_env_name(虚拟环境名称) --all
conda remove --name your_env_name package_name # 删除环境中的某个包
#查看当前环境安装了哪些包
conda list
#安装包
conda install package_name(包名)
conda install scrapy=1.3 # 安装指定版本的包
conda install -n 环境名 包名 # 在conda指定的某个环境中安装包
#查看当前存在哪些虚拟环境
conda env list
#安装requirements.txt依赖
pip install -r requirements.txt
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
更新相关:
#检查更新当前conda
conda update conda
#更新anaconda
conda update anaconda
#更新所有库
conda update --all
#更新python
conda update python
2
3
4
5
6
7
8
# 4.conda和pip安装包区别
①conda install在环境中虽然也可以安装包,但是没有pip安装包管理工具的仓库PyPI提供的包多,因此更多时候还是会使用pip进行安装。
②使用pip安装要注意安装的包会放到哪个环境下,如果使用默认base环境下的pip工具,那么安装的包就会下载到base环境下。如果使用用户自定义env环境的pip工具,才会放到指定的环境下。
第一步先确认当前使用的pip工具为哪一个环境下:
which -a pip
#base环境的pip可能在/root/anaconda3/bin/pip,,,,
#其他conda环境的pip,可能在/root/anaconda3/envs/my_env/bin/pip
2
3
如果不在当前环境下(会默认使用base环境的pip),那么首先需要在当前环境安装pip:
conda install pip
# 5.安装pytorch
查看当前机器显卡的CUDA版本,显存:
nvidia-smi

上官网查询历史pytorch版本安装信息:Previous PyTorch Versions | PyTorch (opens new window)
python与pytorch、cudatoolkit、torchvision之间要保持版本匹配,一般情况下torch和cudatoolkit的版本号要显示指定:
#-c pytorch表示到官网进行下载,可能会比较慢
conda install pytorch==1.10.0 torchvision torchaudio cudatoolkit=11.3 -c pytorch
conda install pytorch==1.10.0 torchvision torchaudio cudatoolkit=11.3
2
3
# 6.本地代码打包
将本地项目打包成zip格式的压缩包,上传到服务器上后,使用如下命令进行解压:
unzip project.zip
# 7.Autodl上使用git科学加速
如果需要使用git下载大文件,那么需要进行git.lfs初始化:
cd /root/autodl-tmp
curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh | sudo bash
sudo apt-get install git-lfs
git lfs install
2
3
4
使用git克隆项目时,需要设置学术加速:
source /etc/network_turbo
git clone ...
2
# 8.其它
如果是使用vscode,在远程服务器上需要安装插件python和pylance。这样可以直接在本地调试服务器远端代码。
调试时需要在.vscode当中的launch.json添加如下配置,才能够对python包的源码进行调试:
{ // 使用 IntelliSense 了解相关属性。 // 悬停以查看现有属性的描述。 // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "Python: 当前文件", "type": "python", "request": "launch", "program": "${file}", "console": "integratedTerminal", "justMyCode": false, "purpose": ["debug-in-terminal"] } ] }1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 9.python小坑
- 类内部定义的方法,需要在形参加上“self”,原因在于通过类对象调用内部函数时,会自动传入self参数,因此如果在类方法声明中,没有给self在形参中占位就会报错。
- 需要使用 . 进行相对导入,那么在当前py文件不能使用main函数。