导入tensorflow报ImportError: DLL load failed: 找不到指定的模块。/ Failed to load the native TensorFlow runtime错误

环境

  • Windows10
  • Anaconda3-2020.02-Windows-x86_64
  • cuda_10.1.243_426.00_win10
  • cudnn-10.1-windows10-x64-v7.6.5.32
  • tensorflow 2.2.0

tensorflow安装

pip install --upgrade tensorflow

在新版本中tensorflow默认支持cpu&gup相当于tensorflow-gpu。如果想要安装仅cpu版的可以使用pip pip install --upgrade tensorflow-cpu

安装过程中没有任何问题,但是在使用import tensorflow as tf时报错。具体错误信息如下。

ImportError                               Traceback (most recent call last)
D:\Program\anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow.py in 
     57
---> 58   from tensorflow.python.pywrap_tensorflow_internal import *
     59

D:\Program\anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py in 
     27             return _mod
---> 28     _pywrap_tensorflow_internal = swig_import_helper()
     29     del swig_import_helper

D:\Program\anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py in swig_import_helper()
     23             try:
---> 24                 _mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
     25             finally:

D:\Program\anaconda3\lib\imp.py in load_module(name, file, filename, details)
    241         else:
--> 242             return load_dynamic(name, filename, file)
    243     elif type_ == PKG_DIRECTORY:

D:\Program\anaconda3\lib\imp.py in load_dynamic(name, path, file)
    341             name=name, loader=loader, origin=path)
--> 342         return _load(spec)
    343

ImportError: DLL load failed: 找不到指定的模块。

During handling of the above exception, another exception occurred:

ImportError                               Traceback (most recent call last)
 in 
----> 1 import tensorflow as tf

D:\Program\anaconda3\lib\site-packages\tensorflow\__init__.py in 
     39 import sys as _sys
     40
---> 41 from tensorflow.python.tools import module_util as _module_util
     42 from tensorflow.python.util.lazy_loader import LazyLoader as _LazyLoader
     43

D:\Program\anaconda3\lib\site-packages\tensorflow\python\__init__.py in 
     48 import numpy as np
     49
---> 50 from tensorflow.python import pywrap_tensorflow
     51
     52 # Protocol buffers

D:\Program\anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow.py in 
     67 for some common reasons and solutions.  Include the entire stack trace
     68 above this error message when asking for help.""" % traceback.format_exc()
---> 69   raise ImportError(msg)
     70
     71 # pylint: enable=wildcard-import,g-import-not-at-top,unused-import,line-too-long

ImportError: Traceback (most recent call last):
  File "D:\Program\anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 58, in 
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "D:\Program\anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 28, in 
    _pywrap_tensorflow_internal = swig_import_helper()
  File "D:\Program\anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 24, in swig_import_helper
    _mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
  File "D:\Program\anaconda3\lib\imp.py", line 242, in load_module
    return load_dynamic(name, filename, file)
  File "D:\Program\anaconda3\lib\imp.py", line 342, in load_dynamic
    return _load(spec)
ImportError: DLL load failed: 找不到指定的模块。


Failed to load the native TensorFlow runtime.

See https://www.tensorflow.org/install/errors

for some common reasons and solutions.  Include the entire stack trace
above this error message when asking for help.

根据提示去https://www.tensorflow.org/install/errors中查找安装时可能会出现的错误信息。恰好在#22794中找到一个类似的错误如下图所示。
导入tensorflow报ImportError: DLL load failed: 找不到指定的模块。/ Failed to load the native TensorFlow runtime错误_第1张图片
上边说如果安装tensorflow-2.1.0或者更高版本的话电脑上需要有Microsoft Visual C++ Redistributable for Visual Studio 2015, 2017 and 2019。这个可以在控制面板\程序\程序和功能中查看有没有。
在这里插入图片描述
如果没有的话就去安装,否则会报错。
导入tensorflow报ImportError: DLL load failed: 找不到指定的模块。/ Failed to load the native TensorFlow runtime错误_第2张图片
下载安装之后重启cmd然后再使用import tensorflow as tf就不会报错了。

至于为什么要安装Microsoft Visual C++ Redistributable for Visual Studio 2015, 2017 and 2019可以在 tensorflow -2.1.0的Release信息中找到答案。
导入tensorflow报ImportError: DLL load failed: 找不到指定的模块。/ Failed to load the native TensorFlow runtime错误_第3张图片

你可能感兴趣的:(tensorflow)