首页 > 解决方案 > ImportError: DLL load failed: 动态链接库 (DLL) 初始化例程失败。(导入 tensorflow 错误)

问题描述

import tensorflow as tf Traceback(最近一次调用最后):文件“E:\path\envs\py36_tf_cpu\lib\site-packages\tensorflow\python\pywrap_tensorflow.py”,第 58 行,来自 tensorflow.python.pywrap_tensorflow_internal import *文件“E:\path\envs\py36_tf_cpu\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py”,第 28 行,在 _pywrap_tensorflow_internal = swig_import_helper() 文件“E:\path\envs\py36_tf_cpu\lib\site- packages\tensorflow\python\pywrap_tensorflow_internal.py”,第 24 行,在 swig_import_helper _mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description) 文件“E:\path\envs\py36_tf_cpu\lib\imp.py”中,第 243 行,在 load_module 返回 load_dynamic(name, filename, file) File "E:\path\envs\py36_tf_cpu\lib\imp.py",第 343 行,在 load_dynamic 返回 _load(spec) ImportError: DLL load failed: 动态链接库 (DLL) 初始化例程失败。

在处理上述异常的过程中,又出现了一个异常:

Traceback(最近一次调用最后):文件“”,第 1 行,在文件“E:\path\envs\py36_tf_cpu\lib\site-packages\tensorflow__init__.py”,第 22 行,in from tensorflow.python import pywrap_tensorflow # pylint :禁用=未使用的导入文件“E:\path\envs\py36_tf_cpu\lib\site-packages\tensorflow\python__init__.py”,第 49 行,从 tensorflow.python 导入 pywrap_tensorflow 文件“E:\path\envs\py36_tf_cpu \lib\site-packages\tensorflow\python\pywrap_tensorflow.py",第 74 行,在 raise ImportError(msg) ImportError: Traceback (last recent call last): File "E:\path\envs\py36_tf_cpu\lib\site- packages\tensorflow\python\pywrap_tensorflow.py”,第 58 行,从 tensorflow.python.pywrap_tensorflow_internal 导入 * 文件“E:\path\envs\py36_tf_cpu\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py”,第 28 行,在 _pywrap_tensorflow_internal = swig_import_helper() 文件“E:\path\envs\py36_tf_cpu\lib\site-packages\tensorflow\ python\pywrap_tensorflow_internal.py”,第 24 行,在 swig_import_helper _mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description) 文件“E:\path\envs\py36_tf_cpu\lib\imp.py”,第 243 行,在load_module return load_dynamic(name, filename, file) File "E:\path\envs\py36_tf_cpu\lib\imp.py", line 343, in load_dynamic return _load(spec) ImportError: DLL load failed: A dynamic link library (DLL ) 初始化例程失败。\path\envs\py36_tf_cpu\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py”,第 24 行,在 swig_import_helper _mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description) 文件“E:\path\ envs\py36_tf_cpu\lib\imp.py”,第 243 行,在 load_module 返回 load_dynamic(name, filename, file) 文件“E:\path\envs\py36_tf_cpu\lib\imp.py”,第 343 行,在 load_dynamic 返回 _load (spec) ImportError: DLL load failed: 动态链接库 (DLL) 初始化例程失败。\path\envs\py36_tf_cpu\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py”,第 24 行,在 swig_import_helper _mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description) 文件“E:\path\ envs\py36_tf_cpu\lib\imp.py”,第 243 行,在 load_module 返回 load_dynamic(name, filename, file) 文件“E:\path\envs\py36_tf_cpu\lib\imp.py”,第 343 行,在 load_dynamic 返回 _load (spec) ImportError: DLL load failed: 动态链接库 (DLL) 初始化例程失败。文件)文件“E:\path\envs\py36_tf_cpu\lib\imp.py”,第 343 行,在 load_dynamic 返回 _load(spec) ImportError: DLL load failed: 动态链接库 (DLL) 初始化例程失败。文件)文件“E:\path\envs\py36_tf_cpu\lib\imp.py”,第 343 行,在 load_dynamic 返回 _load(spec) ImportError: DLL load failed: 动态链接库 (DLL) 初始化例程失败。

无法加载原生 TensorFlow 运行时。

https://www.tensorflow.org/install/install_sources#common_installation_problems

出于一些常见的原因和解决方案。寻求帮助时,请在此错误消息上方包含整个堆栈跟踪。

标签: pythonpython-3.xtensorflow

解决方案


截至 2019 年 4 月,我通过安装CUDA 10.0(不是 10.1 或 9.x !!!)和cuDNN 7.5.0解决了 Windows 10 / Python 3.6.x / GPU RTX 20xx 下的“DLL 加载失败”问题。我还安装了 Visual Studio 2015。

如果您有 CUDA 10.1,请卸载它,安装 10.0,然后重新启动。将来自 cuDNN 的文件放在 CUDA 安装的相应目录中。不要忘记将 cuDNN*.dll文件的位置(/bin/CUDA 安装文件夹)添加到 PATH。

可以使用pip install tensorflow-gpu(当前版本 1.13.1)安装 TensorFlow。

您可以使用以下方法测试 GPU 是否已注册:

import tensorflow as tf
import keras

config = tf.ConfigProto( device_count = {'GPU': 1 , 'CPU': 56} ) 
sess = tf.Session(config=config) 
keras.backend.set_session(sess)

推荐阅读