Fork me on GitHub

import TensorFlow出错解决方案

本文纯属折腾TensorFlow时的一个出错记录。

如果你以前import tensorflow还没有问题,突然import出错的时候,可以进来看看。

出错与解决历程

先介绍一下我的python环境:3.5.2

一开始是tensorflow找不到cudn的几个库,虽然问题不大。看到有教程说要升级tensorflow( 请不要尝试升级 ),于是抱着试试看的想法,升级了tensorflow版本:

pip install –trusted-host pypi.python.org –upgrade tensorflow-gpu

升级之后是1.3.0的最新版本。

接着import tensorflow的时候,出现错误:

Traceback (most recent call last):
  File "C:\Users\Admin\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\Admin\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\Admin\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\Admin\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\Admin\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\Admin\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 "<pyshell#0>", line 1, in <module>
    import tensorflow as tf
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\__init__.py", line 24, in <module>
    from tensorflow.python import *
  File "C:\Users\Admin\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\Admin\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\Admin\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\Admin\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\Admin\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\Admin\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\Admin\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\Admin\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.

查找到相关解决方案:

https://stackoverflow.com/questions/43942185/failed-to-load-the-native-tensorflow-runtime-python-3-5-2

按照高票回答上面做出来后,依旧无效。其中5.1步骤找“curses”,需要pip install curses (从http://www.lfd.uci.edu/~gohlke/pythonlibs/)

简直气疯了。然后,尝试下载了cuda cudnn 9.0的版本,并且加上了系统环境变量,依旧无效。

之后在贴吧http://tieba.baidu.com/p/5147977587 看到11楼说:

我也是同样的问题

是tensorflow-gpu版本不兼容的问题

之前是1.3 后来安装的的1.0.1

亲测有效

于是回退tensorflow的版本。

http://www.lfd.uci.edu/~gohlke/pythonlibs/

下载了1.1.0版本。

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

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:/python/py3_prog/TensorFlow/setup_test.py", line 4, in <module>
    import tensorflow as tf
  File "D:\python\Python35\lib\site-packages\tensorflow\__init__.py", line 24, in <module>
    from tensorflow.python import *
  File "D:\python\Python35\lib\site-packages\tensorflow\python\__init__.py", line 51, in <module>
    from tensorflow.python import pywrap_tensorflow
  File "D:\python\Python35\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 52, in <module>
    raise ImportError(msg)
ImportError: Traceback (most recent call last):
  File "D:\python\Python35\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 41, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "D:\python\Python35\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 35, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "D:\python\Python35\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 30, in swig_import_helper
    _mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)
  File "D:\python\Python35\lib\imp.py", line 242, in load_module
    return load_dynamic(name, filename, file)
  File "D:\python\Python35\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/install_sources#common_installation_problems

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

提示缺少dll。在c/windows/system32中添加了一下msvcp140.dll (680kb的,不是400多kb的)
问题终于解决。

dll问题具体方案:https://www.microsoft.com/en-us/download/details.aspx?id=53587下载Microsoft Visual C++ 2015 Redistributable 并安装。

更多解决办法可参考:https://github.com/tensorflow/tensorflow/issues/8385

总结一下

  • 千万不要去更新tensorflow的版本。能用就行,不要用最新的1.3.0版本。
  • cuda,cudnn也不要用最新的9.0版本,就用8.0版本即可。
  • import tensorflow出错时,有几个方向检查:cudn/cudaa环境变量,dll库的问题,版本是否匹配:python3.5,回退tensorflow版本,回退cuda/cudnn版本。
------ 本文结束感谢您的阅读 ------
坚持原创技术分享,您的支持将鼓励我继续创作!