Python 环境配置
记录一下当前 Python 涉及的一些环境配置。
基本信息
当前本地以及服务器的Python和CUDA的基本信息如下:
- Windows(以及WSL):
- 系统 Windows 11
- miniconda
- Python 版本为 3.12.1
- CUDA 驱动最高支持版本 12.8
- CUDA Toolkit 版本 12.4(
nvcc --version
)
- GPU服务器:
- 系统 Ubuntu 20.04
- anaconda
- Python 版本为 3.12.7
- CUDA 驱动最高支持版本 12.4
- CUDA Toolkit 版本 12.1
为了尽量保持本地和服务器的版本一致,选择使用 Python 3.12.x,CUDA 12.4。 这里并没有刻意保证Python的小版本号一致,直接使用默认的Python版本,反正不同系统的包实际上也有很多区别。
常用包
记录一些常用的包 1
2
3
4
5
6
7
8# 必备
conda install numpy scipy pandas matplotlib seaborn scikit-learn sympy jupyter
# 支持import ipynb文件
pip install import-ipynb
# 进度条
conda install tqdm
VSCode Python 配置
VSCode 与 Python 有关的插件如下:(巨硬把这些插件拆分的实在太细了)
- Python
- Python Debugger
- Pylance:Python语言服务器,支持自动补全、代码提示等
- Black Formatter:代码格式化
- Flake8:代码静态分析
- Jupyter 插件包:
- Jupyter
- Jupyter Keymap
- Jupyter Cell Tags
- Jupyter Notebook Renderers
- Jupyter Slide show
下面是目前VSCode关于Python的配置(2025年3月5日) 1
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
27
28
29
30
31
32
33
34
35
36
37
38
39"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter",
"editor.formatOnType": true,
"editor.unicodeHighlight.allowedLocales": {
"zh-hans": true,
"zh-hant": true
},
},
//[[Python]]
"python.terminal.activateEnvironment": false,
"python.languageServer": "Pylance",
"python.analysis.typeCheckingMode": "basic",
// python格式化基于black(需要单独vscode插件以及conda下载对应模块)
// python静态分析基于flake8(需要单独vscode插件以及conda下载对应模块)
// flake8静态分析的设置 flake8.severity
"flake8.severity": {
"E": "Hint",
"F": "Warning"
},
"black-formatter.args": [
"--verbose",
],
"flake8.showNotifications": "onError",
"python.analysis.diagnosticSeverityOverrides": {
"reportGeneralTypeIssues": "none"
},
"jupyter.askForKernelRestart": false,
"notebook.cellToolbarLocation": {
"default": "right",
"jupyter-notebook": "right"
},
"notebook.markup.fontSize": 16,
"notebook.output.textLineLimit": 50,
"notebook.output.scrolling": true,
"notebook.lineNumbers": "on",
"notebook.outline.showCodeCells": true,
"notebook.outline.showMarkdownHeadersOnly": false,
"notebook.diff.ignoreMetadata": true,
其中大部分都是细节配置,需要注意的是要单独下载 black 和 flake8 包
1
conda install black flake8
如果语法高亮等出现问题,最好先清理相关的配置缓存并重启VSCode,可以解决不少问题。
PyTorch 安装要求 / 安装命令
目前官网的 PyTorch 稳定版为 2.6.0,要求:
- python >= 3.9
- 支持 cuda 12.4、cuda 12.6 或 cpu 版本
- 建议使用 pip 安装,只提供 pip 安装命令,不再提供 conda 安装命令
目前官网提供的几种安装命令如下 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17# (*) windows cuda=12.4
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu124
# windows cuda=12.6
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu126
# windows cpu
pip3 install torch torchvision torchaudio
# (*) linux cuda=12.4
pip3 install torch torchvision torchaudio
# linux cuda=12.6
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu126
# linux cpu
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu
解释一下:
torch
: 是 PyTorch 基础库,提供张量计算和自动求导等基础功能。torchvision
: 是 PyTorch 的一个子模块,专门用于视觉处理任务。torchaudio
: 是 PyTorch 的另一个子模块,专门用于音频处理任务。
conda 环境配置记录
Windows 的 conda 环境 myenv-pytorch 的配置记录:
1 | # 创建环境 |
GPU服务器的 conda 环境 myenv-pytorch-linux 的配置记录:
1 | # 创建环境 |
注意:
- 不要随便升级numpy,这可能导致pytorch无法正常运行。
- 上面两个conda环境中实际选择的numpy版本不一样,可能是python版本和平台差异导致的。
PyTorch 测试脚本
可以跑一段 Python 脚本来检测当前环境安装的 PyTorch
是否正常运行,是否支持使用CUDA,以及检测GPU的信息 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24import torch
# 1. 检查是否有可用的 GPU
if torch.cuda.is_available():
print("GPU 可用")
else:
print("GPU 不可用")
# 2. 获取可用的 GPU 数量
num_gpus = torch.cuda.device_count()
print(f"当前可用的 GPU 数量: {num_gpus}")
# 3. 获取第一个 GPU 的名称
if num_gpus > 0:
gpu_name = torch.cuda.get_device_name(0) # 0 是第一个 GPU
print(f"第一个 GPU 名称: {gpu_name}")
# 4. 获取 GPU 的详细信息
if torch.cuda.is_available():
print(f"GPU 数量: {torch.cuda.device_count()}")
for i in range(torch.cuda.device_count()):
print(f"GPU {i} 名称: {torch.cuda.get_device_name(i)}")
print(f"GPU {i} 内存总量: {torch.cuda.get_device_properties(i).total_memory / (1024 ** 3):.2f} GB")
print(f"GPU {i} 计算能力: {torch.cuda.get_device_properties(i).major}.{torch.cuda.get_device_properties(i).minor}")
例如在个人笔记本的输出 1
2
3
4
5
6
7GPU 可用
当前可用的 GPU 数量: 1
第一个 GPU 名称: NVIDIA GeForce RTX 4060 Laptop GPU
GPU 数量: 1
GPU 0 名称: NVIDIA GeForce RTX 4060 Laptop GPU
GPU 0 内存总量: 8.00 GB
GPU 0 计算能力: 8.9
在GPU服务器上的输出 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22GPU 可用
当前可用的 GPU 数量: 6
第一个 GPU 名称: NVIDIA RTX A6000
GPU 数量: 6
GPU 0 名称: NVIDIA RTX A6000
GPU 0 内存总量: 47.53 GB
GPU 0 计算能力: 8.6
GPU 1 名称: NVIDIA RTX A6000
GPU 1 内存总量: 47.53 GB
GPU 1 计算能力: 8.6
GPU 2 名称: NVIDIA RTX A6000
GPU 2 内存总量: 47.53 GB
GPU 2 计算能力: 8.6
GPU 3 名称: NVIDIA RTX A6000
GPU 3 内存总量: 47.53 GB
GPU 3 计算能力: 8.6
GPU 4 名称: NVIDIA RTX A6000
GPU 4 内存总量: 47.53 GB
GPU 4 计算能力: 8.6
GPU 5 名称: NVIDIA RTX A6000
GPU 5 内存总量: 47.53 GB
GPU 5 计算能力: 8.6
补充
目前在Python项目中使用的.gitignore模板如下 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25__pycache__/
*.py[cod]
*.so
*.dll
*.dylib
.ipynb_checkpoints/
env/
venv/
*.env
*.venv
build/
dist/
*.egg-info/
*.npy
*.npz
*.pt
*.pth
*.log
*.out
*.tmp