导入torch报错OSError WinError 126:fbgemm.dll或其依赖加载失败
WinError 126: fbgemm.dll依赖缺失问题排查与解决
问题背景
通过以下conda命令安装PyTorch环境:
conda install pytorch==2.2.0 torchvision==0.17.0 torchaudio==2.2.0 -c pytorch
后续安装了numpy、pandas、matplotlib、seaborn、scikit-learn、pyedflib,但执行import torch时触发报错,且已重装PyTorch、确认fbgemm.dll文件存在,问题仍未解决。
报错信息
OSError Traceback (most recent call last) Cell In[3], line 1 ----> 1 import torch 2 from torch import nn 3 import numpy as np File d:\anaconda3\envs\RN\lib\site-packages\torch\__init__.py:141 139 err = ctypes.WinError(ctypes.get_last_error()) 140 err.strerror += f' Error loading "{dll}" or one of its dependencies.' --> 141 raise err 143 kernel32.SetErrorMode(prev_error_mode) 146 def _preload_cuda_deps(lib_folder, lib_name): OSError: [WinError 126] can't find this module. Error loading "d:\anaconda3\envs\RN\lib\site-packages\torch\lib\fbgemm.dll" or one of its dependencies.
解决方案
1. 补装VC++运行库
WinError 126多数情况是系统缺少微软VC++ Redistributable运行库,直接安装x64版本的VC++ 2019/2022运行库,安装完成后重启conda环境。
2. 添加PyTorch lib路径到系统环境变量
- 打开系统环境变量设置,找到
Path系统变量 - 添加路径
d:\anaconda3\envs\RN\lib\site-packages\torch\lib,保存后关闭所有终端,重新打开环境测试
3. 换用pip重装PyTorch
先卸载现有PyTorch组件:
conda uninstall pytorch torchvision torchaudio -y
根据硬件选择对应版本安装:
- CPU版本:
pip install torch==2.2.0 torchvision==0.17.0 torchaudio==2.2.0 --index-url https://download.pytorch.org/whl/cpu - CUDA 11.8版本:
pip install torch==2.2.0 torchvision==0.17.0 torchaudio==2.2.0 --index-url https://download.pytorch.org/whl/cu118
4. 定位具体缺失的依赖
使用Dependency Walker工具打开fbgemm.dll,工具会列出所有依赖项,找到标红的缺失文件,针对性安装或补充。
内容的提问来源于stack exchange,提问作者O_Ssom
相关产品推荐
相关产品推荐

