解决Python3.5在Win10下导入Keras/TensorFlow的DLL加载失败问题
在Windows 10系统中使用Python 3.5.2导入Keras时,常因底层TensorFlow依赖的C++运行库缺失而报错。本文提供通过安装Visual C++ 2015更新包并重装TensorFlow的解决方案。
问题现象
在Python 3.5.2环境下尝试导入Keras库时,系统会先加载TensorFlow后端。若TensorFlow未能正确加载,将抛出 ImportError: DLL load failed: The specified module could not be found. 错误。完整的报错堆栈通常包含 _pywrap_tensorflow_internal 模块无法导入的信息,最终提示“Failed to load the native TensorFlow runtime”。
Python 3.5.2 |Continuum Analytics, Inc.| (default, Jul 5 2016, 11:41:13) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import keras
Using TensorFlow backend.
Traceback (most recent call last):
File "d:\Anaconda3\envs\tf\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 18, in swig_import_helper
return importlib.import_module(mname)
File "d:\Anaconda3\envs\tf\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen _bootstrap>", line 986, in _gcd_import
File "<frozen _bootstrap>", line 969, in _find_and_load
File "<frozen _bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen _bootstrap>", line 666, in _load_unlocked
File "<frozen _bootstrap>", line 577, in module_from_spec
File "<frozen _bootstrap_external>", line 906, in create_module
File "<frozen _bootstrap>", line 222, in _call_with_frames_removed
ImportError: DLL load failed: The specified module could not be found.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "d:\Anaconda3\envs\tf\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 41, in <module>
from tensorflow.python.pywrap_tensorflow_internal import *
File "d:\Anaconda3\envs\tf\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 21, in <module>
_pywrap_tensorflow_internal = swig_import_helper()
File "d:\Anaconda3\envs\tf\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 20, in swig_import_helper
return importlib.import_module('_pywrap_tensorflow_internal')
File "d:\Anaconda3\envs\tf\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
ImportError: No module named '_pywrap_tensorflow_internal'
Failed to load the native TensorFlow runtime.
See https://www.tensorflow.org/install/install_sources#common_installation_problems
for some common reasons and solutions. Include the entire stack trace
above this error message when asking for help.解决方法
该错误通常由缺少必要的Microsoft Visual C++运行库引起。请按照以下步骤操作:
第一步:安装Visual C++ 2015 Redistributable
- 访问Microsoft官方提供的ID 53587页面,下载并安装 Microsoft Visual C++ 2015 Redistributable Update 3。
- 安装完成后,务必重启电脑以确保系统环境生效。
第二步:重装TensorFlow
重启后,需卸载现有的TensorFlow相关包并重新安装,以利用新安装的运行库:
# 卸载现有版本
pip uninstall tensorflow
pip uninstall tensorflow-gpu
# 重新安装
pip install tensorflow
pip install tensorflow-gpu若上述步骤仍无效,可尝试升级setuptools后重试:
pip install --upgrade --ignore-installed setuptools验证结果
完成上述操作后,再次在Python环境中执行 import keras。若不再抛出DLL加载失败的错误,且正常输出“Using TensorFlow backend”,则说明问题已解决。
以上就是Python3.5在Win10环境下导入Keras/TensorFlow报错的详细内容,更多关于Python环境配置的资料请关注本站其它相关文章!
本站声明
本文内容仅供参考,不代表本站观点。转载或引用请注明出处。如发现内容问题,请通过联系我们页面反馈。













