动态更新中...

本文最后更新于 2026年1月26日 晚上

配环境

从零开始配环境

第一步:创建环境

打开 anaconda prompt,输入以下命令,切换到项目文件夹

1
(base) C:\Users\xxxx>cd /d "D:\CodeFiles\Project"

命令行变为:

1
(base) D:\CodeFiles\Project>

然后,选择 Python 版本,创建项目环境

1
(base) D:\CodeFiles\Project>conda create -n proj_env python=3.9 -y

会输出:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
3 channel Terms of Service accepted
Retrieving notices: done
Channels:
- defaults
Platform: win-64
Collecting package metadata (repodata.json): done
Solving environment: done

...

The following NEW packages will be INSTALLED:

done
#
# To activate this environment, use
#
# $ conda activate proj_env
#
# To deactivate an active environment, use
#
# $ conda deactivate

第二步:激活环境

激活环境

1
(base) D:\CodeFiles\Project>conda activate proj_env

变成

1
(proj_env) D:\CodeFiles\Project>

第三步:安装依赖

明确给出依赖文件

寻找项目文件夹中的依赖 requirements.txt 或者 environment.yml 文件,输入以下命令安装依赖

1
(proj_env) D:\CodeFiles\Project>pip install -r requirements.txt
1
(proj_env) D:\CodeFiles\Project>conda env update --file environment.yml --prune

特别地,添加国内镜像源可以加速下载。

1
2
3
4
5
6
7
8
# 添加清华源(覆盖pytorch、conda-forge、defaults等核心渠道)
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
# 设为搜索时显示通道地址(可选,方便排查)
conda config --set show_channel_urls yes

添加之后再输入

1
conda env update --file environment.yml --prune

当显示如下信息,就说明我们的环境已经配置成功了!😃

1
2
3
4
5
6
7
8
9
10
11
Successfully installed ...

done
#
# To activate this environment, use
#
# $ conda activate flow_matching
#
# To deactivate an active environment, use
#
# $ conda deactivate
手动安装依赖

如果没有 requirements.txt 或者 environment.yml 文件,可以手动安装依赖。

1
2
3
4
5
6
7
8
9
10
11
# 使用 conda 安装
(proj_env) D:\CodeFiles\Project>conda install tqdm -y

# 一次安装多个包
(proj_env) D:\CodeFiles\Project>conda install tqdm numpy -y

# 或者 pip 安装
(proj_env) D:\CodeFiles\Project>pip install tqdm

# pip 安装可以指定版本
(proj_env) D:\CodeFiles\Project>pip install tqdm==4.66.1

第四步:删除环境

Anaconda Prompt 不能删除当前正在激活的环境,所以先确保你处于 base 环境(默认)或其他非目标删除环境!

如果你当前就在要删除的环境 my_env 中,可以先切换回 base 环境:

1
2
3
4
(my_env) C:\Users\xxxx>conda activate base

# 或者退出环境也行
(my_env) C:\Users\xxxx>conda deactivate

然后查看环境列表

1
(base) C:\Users\xxxx>conda env list

会输出

1
2
3
4
5
# conda environments:
#
base * D:\Anaconda3
my_env D:\Anaconda3\envs\my_env
test_env D:\Anaconda3\envs\test_env

删除 my_env 环境

1
(base) C:\Users\xxxx>conda env remove --name my_env

输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
3 channel Terms of Service accepted

Remove all packages in environment D:\Anaconda\envs\my_env:


## Package Plan ##

environment location: D:\Anaconda\envs\my_env


The following packages will be REMOVED:

bzip2-1.0.8-h2bbff1b_6
...

Proceed ([y]/n)?

选 y 就行。他会继续问你:

1
2
3
4
5
6
7
Downloading and Extracting Packages:

Preparing transaction: done
Verifying transaction: done
Executing transaction: done
Everything found within the environment (D:\Anaconda\envs\my_env), including any conda environment configurations and any non-conda files, will be deleted. Do you wish to continue?
(y/[n])?

继续选 y。

检查环境中的包信息

先激活环境

1
(base) C:\Users\xxxx>conda activate proj_env

查看环境中的包信息

1
(proj_env) D:\CodeFiles\Project>conda list

对 conda 源进行管理

先激活环境

首先查看所有已配置的 conda 源

1
2
3
4
5
# 查看所有已配置的channels(源)
(proj_env) D:\CodeFiles\Project>conda config --show channels

# 可选:查看源的配置文件位置(知道配置存在哪里,方便排查)
# conda config --show-sources

执行后会输出诸如此类的结果

1
2
3
4
5
6
7
channels:
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2/
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r/
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
- defaults

添加pytorch清华源

1
2
3
4
(proj_env) D:\CodeFiles\Project>conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/

# 恢复默认源(保证基础包能下载)
(proj_env) D:\CodeFiles\Project>conda config --add channels defaults

移除单个无效源

1
(proj_env) D:\CodeFiles\Project>conda config --remove channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/

重置

重置所有源

1
(proj_env) D:\CodeFiles\Project>conda config --remove-key channels

可能遇到的坑

CUDA 版本问题

PyTorch 与 CUDA 版本不匹配

错误状况
1
2
3
4
5
6
7
8
9
10
11
D:\Anaconda\envs\proj_env\lib\site-packages\torch\cuda\__init__.py:235: UserWarning: 
NVIDIA GeForce RTX 5070 Laptop GPU with CUDA capability sm_120 is not compatible with the current PyTorch installation.
The current PyTorch install supports CUDA capabilities sm_50 sm_60 sm_61 sm_70 sm_75 sm_80 sm_86 sm_90.
If you want to use the NVIDIA GeForce RTX 5070 Laptop GPU GPU with PyTorch, please check the instructions at https://pytorch.org/get-started/locally/

...

RuntimeError: CUDA error: no kernel image is available for execution on the device
CUDA kernel errors might be asynchronously reported at some other API call, so the stacktrace below might be incorrect.
For debugging consider passing CUDA_LAUNCH_BLOCKING=1
Compile with `TORCH_USE_CUDA_DSA` to enable device-side assertions.
解决方法

先在命令行输入下面指令,查看显卡信息

1
(base) D:\CodeFiles\Project>nvidia-smi

会输出

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
+-----------------------------------------------------------------------------------------+
| NVIDIA-SMI 576.65 Driver Version: 576.65 CUDA Version: 12.9 |
|-----------------------------------------+------------------------+----------------------+
| GPU Name Driver-Model | Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap | Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|=========================================+========================+======================|
| 0 NVIDIA GeForce RTX 5070 ... WDDM | 00000000:01:00.0 On | N/A |
| N/A 45C P8 5W / 115W | 537MiB / 8151MiB | 0% Default |
| | | N/A |
+-----------------------------------------+------------------------+----------------------+

+-----------------------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=========================================================================================|
| 0 N/A N/A 27788 C+G D:\QQ\QQ.exe N/A |
+-----------------------------------------------------------------------------------------+

激活环境

1
(base) D:\CodeFiles\Project>conda activate proj_env

卸载旧版 PyTorch

1
(proj_env) D:\CodeFiles\Project>pip uninstall -y torch

安装新版 PyTorch(CUDA 12.8)

1
2
3
4
(proj_env) D:\CodeFiles\Project>pip3 install torch --index-url https://download.pytorch.org/whl/cu128

# 或者:来自 https://pytorch.org/get-started/locally/
(proj_env) D:\CodeFiles\Project>pip3 install torch torchvision --index-url https://download.pytorch.org/whl/cu128

注释(20260120):不用 conda 是因为 conda 渠道暂未同步 pytorch-cuda=12.8 包,导致PackagesNotFoundError


科研实验记录帖
http://dbqdss.github.io/2025/12/18/杂篇/Little Tricks/科研实验记录帖/
作者
失去理想的獾
发布于
2025年12月18日
许可协议