TensorFlow ImportError:" DLL加载失败"和"没有名为pywrap_tensorflow_internal"的模块

时间:2017-05-31 14:08:58

标签: python python-3.x tensorflow

我正在尝试在Windows 7上安装TensorFlow(不是GPU版本)。我已经安装了Python 3.5.2,我可以验证:

$ python --version
Python 3.5.2

我使用以下命令安装了TensorFlow,它似乎有效:

$ pip3 install --upgrade tensorflow

但是当我进入Python shell来验证安装时,我收到了这个错误:

>>> import tensorflow as tf
Traceback (most recent call last):
  File "C:\Users\kimlesj1\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 18, in swig_import_helper
    return importlib.import_module(mname)
  File "C:\Users\kimlesj1\AppData\Local\Programs\Python\Python35\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 666, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 577, in module_from_spec
  File "<frozen importlib._bootstrap_external>", line 906, in create_module
  File "<frozen importlib._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 "C:\Users\kimlesj1\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 41, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "C:\Users\kimlesj1\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 21, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "C:\Users\kimlesj1\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 20, in swig_import_helper
    return importlib.import_module('_pywrap_tensorflow_internal')
  File "C:\Users\kimlesj1\AppData\Local\Programs\Python\Python35\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
ImportError: No module named '_pywrap_tensorflow_internal'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\kimlesj1\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\__init__.py", line 24, in <module>
    from tensorflow.python import *
  File "C:\Users\kimlesj1\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\__init__.py", line 51, in <module>
    from tensorflow.python import pywrap_tensorflow
  File "C:\Users\kimlesj1\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 52, in <module>
    raise ImportError(msg)
ImportError: Traceback (most recent call last):
  File "C:\Users\kimlesj1\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 18, in swig_import_helper
    return importlib.import_module(mname)
  File "C:\Users\kimlesj1\AppData\Local\Programs\Python\Python35\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 666, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 577, in module_from_spec
  File "<frozen importlib._bootstrap_external>", line 906, in create_module
  File "<frozen importlib._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 "C:\Users\kimlesj1\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 41, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "C:\Users\kimlesj1\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 21, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "C:\Users\kimlesj1\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 20, in swig_import_helper
    return importlib.import_module('_pywrap_tensorflow_internal')
  File "C:\Users\kimlesj1\AppData\Local\Programs\Python\Python35\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.

3 个答案:

答案 0 :(得分:1)

就我而言,cudnn v5或v6无法单独工作。我查看了自检脚本,似乎检查了cudnn64_5.dll和cudnn64_6.dll的正确安装:

cudnn5_found = False
try:
  cudnn5 = ctypes.WinDLL("cudnn64_5.dll")
  cudnn5_found = True
except OSError:
  candidate_explanation = True
  print("""
  - Could not load 'cudnn64_5.dll'. The GPU version of TensorFlow
  requires that this DLL be installed in a directory that is named in
  your %PATH% environment variable. Note that installing cuDNN is a
  separate step from installing CUDA, and it is often found in a
  different directory from the CUDA DLLs. You may install the
  necessary DLL by downloading cuDNN 5.1 from this URL:
  https://developer.nvidia.com/cudnn""")

cudnn6_found = False
try:
  cudnn = ctypes.WinDLL("cudnn64_6.dll")
  cudnn6_found = True
except OSError:
  candidate_explanation = True

if not cudnn5_found or not cudnn6_found:
  print()
  if not cudnn5_found and not cudnn6_found:
    print("- Could not find cuDNN.")
  elif not cudnn5_found:
   print("- Could not find cuDNN 5.1.")
else:
  print("- Could not find cuDNN 6.")
  print("""
  The GPU version of TensorFlow requires that the correct cuDNN DLL be 
  installed
  in a directory that is named in your %PATH% environment variable. Note 
  that
  installing cuDNN is a separate step from installing CUDA, and it is often
  found in a different directory from the CUDA DLLs. The correct version of
  cuDNN depends on your version of TensorFlow:

  * TensorFlow 1.2.1 or earlier requires cuDNN 5.1. ('cudnn64_5.dll')
  * TensorFlow 1.3 or later requires cuDNN 6. ('cudnn64_6.dll')

如果在Path中找不到v5或v6,则会发生OSError。所以我把它们放在我的Path环境变量中,并且检查通过了。

答案 1 :(得分:0)

脚本tensorflow_self_check.py在我的情况下完美运行。它指出我错过了cuDNN v6的文件cudnn64_6.dll。重要的是要注意在TensorFlow for Window(https://www.tensorflow.org/install/install_windows)的官方指南中,他们坚持认为它必须是带有cuDNN64_5.dll的cuDNN v5.1!他们应该通过添加此tensorflow_self_check.py脚本来更新本指南。

编辑:我应该仔细阅读TensorFlow 1.3.0发布说明https://github.com/tensorflow/tensorflow/blob/r1.3/RELEASE.md:&#34;我们所有的预建二进制文件都是用cuDNN 6构建的。我们期待用cuDNN 7发布TensorFlow 1.4。&#34 ;

答案 2 :(得分:0)

这是安装tensorflow-gpu时的已知问题。

如果您使用的是conda,则解决此问题的最佳方法是执行tensorflow-gpu的conda安装。步骤如下。 (已在Windows 10和Ubuntu 16.04中测试)

卸载现有的tensorflow-gpu安装

pip uninstall tensorflow-gpu

然后使用conda安装tensorflow-gpu

conda install tensorflow-gpu

这应该在所有依赖项的conda环境中安装tensorflow-gpu。

如果您未使用python的anaconda发行版,则可以尝试使用正确版本的cudatoolkit,CuDNN和python。可以在此处找到常见错误及其对应的github线程以及解决方案的列表。

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

如果您不想花很多时间弄清楚问题出在哪里以及如何解决它,我建议您使用 conda安装而不是 pip安装