开发者 / 数据科学
Python / Anaconda 报 DLL load failed
pip install 后 import 包时报 "DLL load failed while importing xxx"
典型症状
- ImportError: DLL load failed while importing _ssl
- ImportError: DLL load failed: 找不到指定的模块
- numpy / scipy / torch / tensorflow 导入失败
- Conda 环境激活后跑 Python 立即崩溃
官方安全修复步骤
-
安装最新 VC++ 2015-2022 Redistributable
Python 3.5+ 是用 VS 2015+ 编译的,所有用 C 扩展的包(numpy、scipy、torch、pillow、psycopg2 等)都需要 vcruntime140。x64 Python 装 x64 redist,x86 Python 装 x86 redist。
-
检查 Python 位数与扩展包是否匹配
PowerShell 跑 python -c "import struct; print(struct.calcsize(\"P\")*8)" 查看 Python 位数(32 或 64)。再确认你 pip install 的轮子(.whl)也是同位数。
python -c "import struct; print(struct.calcsize('P') * 8)" -
用 conda 重新创建环境而不是修复
Conda 环境出问题时,conda create -n new_env --clone broken_env 之后删掉旧的,比手工修 DLL 高效得多。
-
用 Process Monitor 抓加载失败
高级用法:用 SysInternals 的 Process Monitor 过滤 python.exe + Result is NOT SUCCESS,能精确看到它在哪一步加载哪个 DLL 失败,比报错信息有用得多。