【问题标题】:TensorFlow ImportError: "DLL load failed" and "No module named pywrap_tensorflow_internal"TensorFlow ImportError:“DLL 加载失败”和“没有名为 pywrap_tensorflow_internal 的模块”
【发布时间】:2017-05-31 14:08:57
【问题描述】:

我正在尝试在 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.

【问题讨论】:

  • 我们创建了一个脚本来帮助调试此类错误。您可以尝试下载并运行代码here 并遵循其建议。 (由于你没有使用GPU版本,所以可以忽略关于CUDA的警告……很可能是找不到msvcp140.dll库,可以从here安装。)
  • 谢谢!你是对的,我错过了msvcp140.dll,安装后可以使用 TensorFlow。
  • 我也有同样的问题,我没有错过这个 DLL。
  • 我已经安装了来自 Microsoft Visual C++ 2015 Redistributable Update 3 的 msvcp140.dll,但仍然存在同样的问题。

标签: python python-3.x tensorflow


【解决方案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 环境变量中,并且检查通过了。

【讨论】:

    【解决方案2】:

    这是安装 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 install 而不是 pip install

    【讨论】:

      【解决方案3】:

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

      编辑:我应该仔细阅读 TensorFlow 1.3.0 https://github.com/tensorflow/tensorflow/blob/r1.3/RELEASE.md 的发行说明:“我们所有预构建的二进制文件都是使用 cuDNN 6 构建的。我们预计会发布带有 cuDNN 7 的 TensorFlow 1.4。”

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-03
        • 1970-01-01
        • 2021-03-04
        • 2017-08-01
        • 2020-05-07
        相关资源
        最近更新 更多