Python 无法在 Windows 10 上为 GPU 导入 Tensorflow

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43577923/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:12:31  来源:igfitidea点击:

Cannot import Tensorflow for GPU on Windows 10

pythondlltensorflow

提问by snharan91

Trying to install the gpu version of Tensorflow on my machine, but I'm getting this error when trying 'import tensorflow'

尝试在我的机器上安装 GPU 版本的 Tensorflow,但在尝试“导入 tensorflow”时出现此错误



ImportError Traceback (most recent call last) C:\Users\snh5_000\Anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflo w_internal.py in swig_import_helper() 17 try: ---> 18 return importlib.import_module(mname) 19 except ImportError:

C:\Users\snh5_000\Anaconda3\lib\importlib__init__.py in import_module(name, pac kage) 125 level += 1 --> 126 return _bootstrap._gcd_import(name[level:], package, level) 127

C:\Users\snh5_000\Anaconda3\lib\importlib_bootstrap.py in _gcd_import(name, pac kage, level)

C:\Users\snh5_000\Anaconda3\lib\importlib_bootstrap.py in _find_and_load(name, import_)

C:\Users\snh5_000\Anaconda3\lib\importlib_bootstrap.py in _find_and_load_unlock ed(name, import_)

C:\Users\snh5_000\Anaconda3\lib\importlib_bootstrap.py in _load_unlocked(spec)

C:\Users\snh5_000\Anaconda3\lib\importlib_bootstrap.py in module_from_spec(spec )

C:\Users\snh5_000\Anaconda3\lib\importlib_bootstrap_external.py in create_modul e(self, spec)

C:\Users\snh5_000\Anaconda3\lib\importlib_bootstrap.py in _call_with_frames_rem oved(f, *args, **kwds)

ImportError: DLL load failed: The specified module could not be found.

During handling of the above exception, another exception occurred:

ImportError Traceback (most recent call last) C:\Users\snh5_000\Anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflo w.py in () 40 sys.setdlopenflags(_default_dlopen_flags | ctypes.RTLD_GLOBAL) ---> 41 from tensorflow.python.pywrap_tensorflow_internal import * 42 from tensorflow.python.pywrap_tensorflow_internal import version

C:\Users\snh5_000\Anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflo w_internal.py in () 20 return importlib.import_module('_pywrap_tensorflow_internal' ) ---> 21 _pywrap_tensorflow_internal = swig_import_helper() 22 del swig_import_helper

C:\Users\snh5_000\Anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflo w_internal.py in swig_import_helper() 19 except ImportError: ---> 20 return importlib.import_module('_pywrap_tensorflow_internal' ) 21 _pywrap_tensorflow_internal = swig_import_helper()

C:\Users\snh5_000\Anaconda3\lib\importlib__init__.py in import_module(name, pac kage) 125 level += 1 --> 126 return _bootstrap._gcd_import(name[level:], package, level) 127

ImportError: No module named '_pywrap_tensorflow_internal'

During handling of the above exception, another exception occurred:

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

C:\Users\snh5_000\Anaconda3\lib\site-packages\tensorflow__init__.py in () 22 23 # pylint: disable=wildcard-import ---> 24 from tensorflow.python import * 25 # pylint: enable=wildcard-import 26

C:\Users\snh5_000\Anaconda3\lib\site-packages\tensorflow\python__init__.py in < module>() 49 import numpy as np 50 ---> 51 from tensorflow.python import pywrap_tensorflow 52 53 # Protocol buffers

C:\Users\snh5_000\Anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflo w.py in () 50 for some common reasons and solutions. Include the entire stack trace 51 above this error message when asking for help.""" % traceback.format_exc () ---> 52 raise ImportError(msg) 53 54 # pylint: enable=wildcard-import,g-import-not-at-top,unused-import,line- too-long

ImportError: Traceback (most recent call last): File "C:\Users\snh5_000\Anaconda3\lib\site-packages\tensorflow\python\pywrap_t ensorflow_internal.py", line 18, in swig_import_helper return importlib.import_module(mname) File "C:\Users\snh5_000\Anaconda3\lib\importlib__init__.py", line 126, in imp ort_module return _bootstrap._gcd_import(name[level:], package, level) File "", line 986, in _gcd_import File "", line 969, in _find_and_load File "", line 958, in _find_and_load_unlocked File "", line 666, in _load_unlocked
File "", line 577, in module_from_spec
File "", line 906, in create_module File "", 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\snh5_000\Anaconda3\lib\site-packages\tensorflow\python\pywrap_t ensorflow.py", line 41, in from tensorflow.python.pywrap_tensorflow_internal import * File "C:\Users\snh5_000\Anaconda3\lib\site-packages\tensorflow\python\pywrap_t ensorflow_internal.py", line 21, in _pywrap_tensorflow_internal = swig_import_helper() File "C:\Users\snh5_000\Anaconda3\lib\site-packages\tensorflow\python\pywrap_t ensorflow_internal.py", line 20, in swig_import_helper return importlib.import_module('_pywrap_tensorflow_internal') File "C:\Users\snh5_000\Anaconda3\lib\importlib__init__.py", line 126, in imp ort_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.

ImportError Traceback(最近一次调用最后一次)C:\Users\snh5_000\Anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow w_internal.py in swig_import_helper() 17 try: ---> 18 return importlib.import_module(mname) 19 除了导入错误:

C:\Users\snh5_000\Anaconda3\lib\importlib__init__.py in import_module(name, pack) 125 level += 1 --> 126 return _bootstrap._gcd_import(name[level:], package, level) 127

C:\Users\snh5_000\Anaconda3\lib\importlib_bootstrap.py 中的_gcd_import(名称,包,级别)

C:\Users\snh5_000\Anaconda3\lib\importlib_bootstrap.py in _find_and_load(name, import_)

C:\Users\snh5_000\Anaconda3\lib\importlib_bootstrap.py in _find_and_load_unlock ed(name, import_)

C:\Users\snh5_000\Anaconda3\lib\importlib_bootstrap.py in _load_unlocked(spec)

C:\Users\snh5_000\Anaconda3\lib\importlib_bootstrap.py in module_from_spec(spec)

C:\Users\snh5_000\Anaconda3\lib\importlib_bootstrap_external.py in create_module e(self, spec)

C:\Users\snh5_000\Anaconda3\lib\importlib_bootstrap.py in _call_with_frames_rem oved(f, *args, **kwds)

导入错误:DLL 加载失败:找不到指定的模块。

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

ImportError Traceback(最近一次调用)C:\Users\snh5_000\Anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow.py in () 40 sys.setdlopenflags(_default_dlopen_flags | ctypes.RTLD_GLOBAL) ---> 41来自 tensorflow.python.pywrap_tensorflow_internal 导入 * 42 来自 tensorflow.python.pywrap_tensorflow_internal 导入版本

C:\Users\snh5_000\Anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow\python\pywrap_tensorflow_internal.py in () 20 return importlib.import_module('_pywrap_tensorflow_internal' ) ---> 21 _pywrap_tensorflow_internal = swig_import_import_help ()

C:\Users\snh5_000\Anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow\python\pywrap_tensorflow w_internal.py in swig_import_helper() 19 除了 ImportError: ---> 20 return importlib.import_module('_pywrap_tensorflow_internal' ) 21 )

C:\Users\snh5_000\Anaconda3\lib\importlib__init__.py in import_module(name, pack) 125 level += 1 --> 126 return _bootstrap._gcd_import(name[level:], package, level) 127

导入错误:没有名为“_pywrap_tensorflow_internal”的模块

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

ImportError Traceback(最近一次调用最后一次) in () ----> 1 import tensorflow

C:\Users\snh5_000\Anaconda3\lib\site-packages\tensorflow__init__.py in () 22 23 # pylint: disable=wildcard-import ---> 24 from tensorflow.python import * 25 # pylint: enable=wildcard-进口 26

C:\Users\snh5_000\Anaconda3\lib\site-packages\tensorflow\python__init__.py in <module>() 49 import numpy as np 50 ---> 51 from tensorflow.python import pywrap_tensorflow 52 53 # Protocol buffers

C:\Users\snh5_000\Anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow w.py in () 50 中的一些常见原因和解决方案。请求帮助时在此错误消息上方包含整个堆栈跟踪 51。""" % traceback.format_exc () ---> 52 raise ImportError(msg) 53 54 # pylint: enable=wildcard-import,g-import-not -at-top,unused-import,line-太长

导入错误:回溯(最近一次调用):文件“C:\Users\snh5_000\Anaconda3\lib\site-packages\tensorflow\python\pywrap_t ensorflow_internal.py”,第 18 行,在 swig_import_helper 中返回 importlib.import_module(mname) 文件"C:\Users\snh5_000\Anaconda3\lib\importlib__init__.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "", line 986, in _gcd_import File "" , 第 969 行,在 _find_and_load File "",第 958 行,在 _find_and_load_unlocked File "",第 666 行,在 _load_unlocked
File "",第 577 行,在 module_from_spec
File "",第 906行,在 create_module File "",第 222 行, _call_with_frames_removed 导入错误:DLL 加载失败:指定的模块无法找到。

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

回溯(最近一次调用):文件“C:\Users\snh5_000\Anaconda3\lib\site-packages\tensorflow\python\pywrap_t ensorflow.py”,第 41 行,来自 tensorflow.python.pywrap_tensorflow_internal import * 文件“C :\Users\snh5_000\Anaconda3\lib\site-packages\tensorflow\python\pywrap_t ensorflow_internal.py”,第 21 行,在 _pywrap_tensorflow_internal = swig_import_helper() 文件“C:\Users\snh5_000\Anaconda3s tensorflow\python\pywrap_t ensorflow_internal.py”,第 20 行,在 swig_import_helper 中 return importlib.import_module('_pywrap_tensorflow_internal') 文件“C:\Users\snh5_000\Anaconda3\lib\importlib__init__.py”,第 126 行,在 return_boot_strap 中._gcd_import(name[level:], package, level) ImportError: No module named '_pywrap_tensorflow_internal'

无法加载本机 TensorFlow 运行时。

请参阅 https://www.tensorflow.org/install/install_sources#common_installation_problems

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

Running Windows 10, Python 3.5.2, Anaconda 4.2.0 64-bit. Installed tensorflow-gpu using 'pip install tensorflow-gpu'.

运行 Windows 10、Python 3.5.2、Anaconda 4.2.0 64 位。使用 'pip install tensorflow-gpu' 安装了 tensorflow-gpu。

I've attempted the fixes in https://github.com/tensorflow/tensorflow/issues/5949, namely running Microsoft Visual C++ 2015 Redistributable Update 3 and confirming I had the following DLLs in System32/SysWOW64/my Python folder:

我已经尝试了https://github.com/tensorflow/tensorflow/issues/5949 中的修复,即运行 Microsoft Visual C++ 2015 Redistributable Update 3 并确认我在 System32/SysWOW64/my Python 文件夹中有以下 DLL:

KERNEL32.dll

WSOCK32.dll

WS2_32.dll

SHLWAPI.dll

python35.dll

MSVCP140.dll

VCRUNTIME140.dll

api-ms-win-crt-runtime-l1-1-0.dll

api-ms-win-crt-heap-l1-1-0.dll

api-ms-win-crt-utility-l1-1-0.dll

api-ms-win-crt-stdio-l1-1-0.dll

api-ms-win-crt-string-l1-1-0.dll

api-ms-win-crt-math-l1-1-0.dll

api-ms-win-crt-convert-l1-1-0.dll

api-ms-win-crt-environment-l1-1-0.dll

api-ms-win-crt-filesystem-l1-1-0.dll

api-ms-win-crt-time-l1-1-0.dll

内核32.dll

WSOCK32.dll

WS2_32.dll

SHLWAPI.dll

python35.dll

MSVCP140.dll

VCRUNTIME140.dll

api-ms-win-crt-runtime-l1-1-0.dll

api-ms-win-crt-heap-l1-1-0.dll

api-ms-win-crt-utility-l1-1-0.dll

api-ms-win-crt-stdio-l1-1-0.dll

api-ms-win-crt-string-l1-1-0.dll

api-ms-win-crt-math-l1-1-0.dll

api-ms-win-crt-convert-l1-1-0.dll

api-ms-win-crt-environment-l1-1-0.dll

api-ms-win-crt-filesystem-l1-1-0.dll

api-ms-win-crt-time-l1-1-0.dll

It's worth noting that the cpu version of Tensorflow imports and runs just fine, but when I attempt to use the gpu version I get the above error. I presume this is due to a mistake on my part in configuring the CUDA toolkit/cudNN, but I think I did everything listed in the instructions correctly. I made sure I had an entry in the PATH environment variable for the CUDA toolkit (leading to \bin and \libnvvp) and added one in for cudNN (leading to \cuda).

值得注意的是,Tensorflow 的 cpu 版本导入并运行得很好,但是当我尝试使用 gpu 版本时,出现上述错误。我认为这是由于我在配置 CUDA 工具包/cudNN 时出错,但我认为我正确地完成了说明中列出的所有内容。我确保在 PATH 环境变量中为 CUDA 工具包(导致 \bin 和 \libnvvp)有一个条目,并为 cudNN(导致 \cuda)添加了一个条目。

Any thoughts or other information you'd need to help?

您有什么想法或其他信息需要帮助吗?

回答by Nova

Try to rename cudnn64_6.dllto cudnn64_5.dllin C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\bin.

尝试重命名cudnn64_6.dllcudnn64_5.dllin C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\bin

回答by Amar Sharma

You need to have CUDA_HOMEleading to \cudaand %CUDA_HOME%\binappended to your PATH. Also, tensorflow as of now only supports cudNN 5.1, please make sure you have that.

你需要有CUDA_HOME引导\cuda%CUDA_HOME%\bin附加到你的路径。此外,目前 tensorflow 仅支持 cudNN 5.1,请确保您拥有该版本。

回答by Jean Louw

Have you tried turning it off and on again?Seriously, though. If you have your command line terminal running during the CUDA and cuDNN installs, the environment variables aren't refreshed until you close and re-open the terminal. I know it's simple, but I struggled for an hour before a simple restart solved that same import error.

你试过把它关掉再打开吗?不过说真的。如果您在 CUDA 和 cuDNN 安装期间运行命令行终端,则环境变量不会刷新,直到您关闭并重新打开终端。我知道这很简单,但在简单的重启解决了同样的导入错误之前,我挣扎了一个小时。

回答by mrdatamx

I had the same problem running Windows 10, Anaconda 4.3.21 64bit, Python 3.5.3, and TensorFlow 1.2.0.
However, the issue was fixed by installing cuDNN v5.1 instead of v6.0 as proposed here https://stackoverflow.com/a/43566341/3830240

我在运行 Windows 10、Anaconda 4.3.21 64 位、Python 3.5.3 和 TensorFlow 1.2.0 时遇到了同样的问题。
但是,该问题已通过安装 cuDNN v5.1 而不是 v6.0 解决,如此处所提议的https://stackoverflow.com/a/43566341/3830240

回答by aldel

I still got the _pywrap_tensorflow_internalerror after installing cuDNN 5.1 (yes, I had PATH set correctly). I had better luck installing tensorflow_gpu from this site. This requires cuDNN 6.0, not 5.1. Unfortunately they currently only have Tensorflow 1.1 for GPU systems.

_pywrap_tensorflow_internal安装 cuDNN 5.1 后我仍然遇到错误(是的,我正确设置了 PATH)。我有幸从这个站点安装 tensorflow_gpu 。这需要 cuDNN 6.0,而不是 5.1。不幸的是,他们目前只有用于 GPU 系统的 Tensorflow 1.1。

回答by Gásten

I tried all the solutions here and it wasn't working for me. Check this linkfor the requirements and make sure you have the compatible versions. I also added all the cuDNN paths to my environment variables and also don't forget to copy the contents of lib,binand includeto the same folders inside your CUDA installation.

我在这里尝试了所有解决方案,但对我不起作用。检查此链接以了解要求并确保您拥有兼容的版本。我还将所有 cuDNN 路径添加到我的环境变量中,并且不要忘记将libbininclude的内容复制到 CUDA 安装中的相同文件夹中。

回答by Hani

This is what worked for me. I referred to the Tensorflow website to find the currently supported CudaNN version. When I solved my issue, it was CudaNN 6.x. The latest version, in my case 7 DLL is not loaded by tensorflow, and if you try to rename it, it causes memory errors.

这对我有用。我参考了 Tensorflow 网站找到了当前支持的 CudaNN 版本。当我解决我的问题时,它是 CudaNN 6.x。最新版本,在我的例子中 7 DLL 没有被 tensorflow 加载,如果你尝试重命名它,它会导致内存错误。

回答by Rika

It should be noted that starting from version 1.0 up until version 1.2 Tensorflowneeded Cuda8 and cudnnv5.1. Therefore You need to have cudnnv5.1 installed.
This question was asked on April 24 2017, This means the OP was trying to install version 1.1.0(at the time, the latest version) which at the time needed cudnnv5.1 and python3.5. So he needed to have cudnnv5.1 to fix his/her problem.

需要注意的是,从 1.0 版到 1.2 版都Tensorflow需要 Cuda8 和cudnnv5.1. 因此您需要安装 cudnnv5.1。
这个问题是在 上提出的April 24 2017,这意味着 OP 正在尝试安装 1.1.0 版(当时是最新版本),当时需要 cudnnv5.1 和 python3.5。所以他需要有 cudnnv5.1 来解决他/她的问题。

However, if you are trying to install the latest version of Tensorflow, that is anything greater than 1.3.0, you need cudnnv6.0+.
If you are getting this error on 1.3.0, then it means you need cudnnv6.0. put that into your PATHand you will be fine.

但是,如果您尝试安装Tensorflow1.3.0 以上的最新版本,则需要cudnnv6.0+。
如果您在 1.3.0 上收到此错误,则意味着您需要 cudnnv6.0。把它放进你的PATH,你会没事的。

回答by Khin Maung Htay

It didn't work for me trying to change dll file versions. After struggling with tensorflow-gpu import error for a couple of weeks, I finally figured out that I needed to make respective directories(CUDA,cuDNN & CUPTI) matched to my %PATH% environment variable. I am using CUDA 9.0, cuDNN v7.0 with Tensorflow 1.11.

尝试更改 dll 文件版本对我不起作用。在为 tensorflow-gpu 导入错误挣扎了几个星期之后,我终于发现我需要使相应的目录(CUDA、cuDNN 和 CUPTI)与我的 %PATH% 环境变量相匹配。我正在使用 CUDA 9.0、cuDNN v7.0 和 Tensorflow 1.11。

C:\> SET PATH=C:\Program Files\NVIDIA GPU Computing
Toolkit\CUDA\v9.0\bin;%PATH% 

C:\> SET PATH=C:\Program Files\NVIDIA GPU
Computing Toolkit\CUDA\v9.0\extras\CUPTI\libx64;%PATH% 

C:\> SET PATH=C:\tools\cuda\bin;%PATH%

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

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