TensorFlow导入失败求助:Windows10下DLL加载及模块缺失问题
问题背景
你在Windows 10家庭版64位系统上,以管理员身份安装了Python 3.6.5,通过pip install tensorflow成功完成安装后,执行import tensorflow as tf时出现了DLL加载失败及找不到_pywrap_tensorflow_internal模块的错误,完整报错堆栈如下:
Traceback (most recent call last): File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 14, in swig_import_helper return importlib.import_module(mname) File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\importlib__init__.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "", line 994, in _gcd_import File "", line 971, in _find_and_load File "", line 955, in _find_and_load_unlocked File "", line 658, in _load_unlocked
File "", line 571, in module_from_spec
File "", line 922, in create_module File "", line 219, in _call_with_frames_removed ImportError: DLL加载失败:动态链接库(DLL)初始化例程失败。During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 58, in from tensorflow.python.pywrap_tensorflow_internal import * File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 17, in pywrap_tensorflow_internal = swig_import_helper() File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 16, in swig_import_helper return importlib.import_module('pywrap_tensorflow_internal') File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\importlib__init.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) ModuleNotFoundError: No module named '_pywrap_tensorflow_internal'
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "", line 1, in import tensorflow as tf File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow__init__.py", line 24, in from tensorflow.python import pywrap_tensorflow # pylint: disable=unused-import File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python__init__.py", line 49, in from tensorflow.python import pywrap_tensorflow File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 74, in raise ImportError(msg) ImportError: Traceback (most recent call last): File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 14, in swig_import_helper return importlib.import_module(mname) File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\importlib__init__.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "", line 994, in _gcd_import File "", line 971, in _find_and_load File "", line 955, in _find_and_load_unlocked File "", line 658, in _load_unlocked
File "", line 571, in module_from_spec
File "", line 922, in create_module File "", line 219, in _call_with_frames_removed ImportError: DLL加载失败:动态链接库(DLL)初始化例程失败。During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 58, in from tensorflow.python.pywrap_tensorflow_internal import * File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 17, in pywrap_tensorflow_internal = swig_import_helper() File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 16, in swig_import_helper return importlib.import_module('pywrap_tensorflow_internal') File "C:\Users\User\AppData\Local\Programs\Python\Python36\lib\importlib__init.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) ModuleNotFoundError: No module named '_pywrap_tensorflow_internal'
Failed to load the native TensorFlow runtime.
解决方案
针对这个Windows环境下常见的TensorFlow导入问题,我整理了几个实用的解决方法,你可以按顺序尝试:
安装Visual C++ Redistributable运行库
TensorFlow依赖微软的Visual C++ 2015-2019 Redistributable包,很多Windows系统默认没有安装完整版本。你需要下载对应64位的安装包(和系统位数匹配),安装完成后重启电脑,再尝试导入TensorFlow。匹配TensorFlow与Python的兼容版本
Python 3.6.5和TensorFlow 2.x版本兼容性较差,建议降级到TensorFlow 1.x系列的稳定版本,比如1.15.5。执行以下命令重新安装:pip uninstall tensorflow -y pip install tensorflow==1.15.5检查CPU是否支持AVX指令集
新版TensorFlow默认要求CPU支持AVX指令集,如果你的CPU是2015年之前发布的老旧型号,可能不支持该指令集。这时需要安装不依赖AVX的TensorFlow版本,你可以找到第三方编译的.whl文件,通过pip install 本地路径/文件名.whl完成安装。检查Python安装路径与环境变量
确保Python的安装路径(包括Scripts目录)已添加到系统环境变量中,且安装时使用了管理员权限。另外,避免将Python安装在带有中文、空格或特殊字符的路径下,这会导致DLL加载失败。
内容的提问来源于stack exchange,提问作者ChrisC

