Python中安装PyTorch失败求助:提示python313.dll缺失及编译错误
问题背景
执行pip install pytorch时触发编译错误,同时弹出python313.dll缺失提示,已更新Microsoft VC++ 2015-2022运行库并重启,问题未解决。
执行命令
pip install pytorch
错误详情
终端日志
Collecting pytorch
Using cached pytorch-1.0.2.tar.gz (689 bytes)
Installing build dependencies ... done
Getting requirements to build wheel ... error
error: subprocess-exited-with-error× Getting requirements to build wheel did not run successfully.
│ exit code: 3221225781
╰─> [0 lines of output]
[end of output]note: This error originates from a subprocess, and is likely not a problem with pip.
error: subprocess-exited-with-error× Getting requirements to build wheel did not run successfully.
│ exit code: 3221225781
╰─> See above for output.note: This error originates from a subprocess, and is likely not a problem with pip.
系统弹窗
无法继续执行代码,因为找不到python313.dll。重新安装程序可能会解决此问题。
修复步骤
替换为PyTorch官方预编译包安装命令
pip install pytorch会拉取过时的源码包尝试本地编译,这是错误根源。使用对应环境的预编译包命令:- CPU版本:
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu - CUDA 12.1版本:
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121
(根据实际CUDA版本调整
cu121为cu118/cu124等对应标签)- CPU版本:
修复python313.dll缺失
- 确认Python安装目录已添加至系统
PATH环境变量,且该目录下存在python313.dll - 若dll缺失,下载与当前Python版本、架构(32/64位)完全匹配的安装包,执行修复安装(保留现有环境)
- 修复后仍异常的话,复制Python安装目录下的
python313.dll到C:\Windows\System32(64位系统)或C:\Windows\SysWOW64(32位系统)
- 确认Python安装目录已添加至系统
验证安装有效性
运行以下代码确认PyTorch正常工作:import torch print(torch.__version__) print(torch.cuda.is_available())
内容的提问来源于stack exchange,提问作者tongu ram

