安装 Tensorflow 2 导致 dll 加载失败 pywrap_tensorflow.py

Installing Tensorflow 2 gets a dll failed to load in pywrap_tensorflow.py

在过去一年左右的时间里,我 here, here and there 安装了 TensorFlow 2。所以我正在尝试 Miniconda。

我有一个 AMD Radeon hd 6670 和一个 AMD Radeon hd 6450。

我刚刚下载了 Miniconda 并制作了一个环境,并在 Windows 8.1 上的 Miniconda 提示符下做了 pip install --upgrade tensorflow 并获得了 TensorFlow 2.2。

当我尝试导入 tensorflow 时,我得到了下面的堆栈跟踪。

我确实下载了 Visual Studio 以获取最新的再分发包(我认为)。

似乎这发生在这条线附近:from tensorflow.python.pywrap_tensorflow_internal import *

编辑 1:我将此 yaml 文件用于 python 3.6(另一个是 3.7),但它产生了 相同的 错误。

编辑 2:我升级到 Conda 4.8.3 和 Python 3.7(在 yaml 文件中)并得到同样的错误。这是 pywrap 内部显示问题的行:

line 28: _pywrap_tensorflow_internal = swig_import_helper()

在 swig 助手中:

fp, pathname, description = imp.find_module('_pywrap_tensorflow_internal', [dirname(__file__)])

它似乎在努力寻找自己? 有人知道吗?

编辑 3: looks like the same problem, but it's dated. This 似乎与使用 2015 C++ 可再发行组件修复的问题相同。我确实安装了 2015 x64 和 2015-2019 x86。

编辑 4:尝试 3.5 出现子流程错误(请参阅下文)。

编辑 5:尝试使用 3.7:conda install 'tensorflow=*=mkl*'(并且没有 yaml 文件)确实有效(3.6 有效)。不幸的是,导入 tf2 仍然需要 10 秒!

搜索:-gpu swig_import_helper“无法加载本机 TensorFlow 运行时”导致约 3.6k 次点击,因此这是一个持续存在的问题。

编辑 5:安装 Keras 因 this 错误而中断。尝试修复得到:AttributeError: module 'tensorflow' has no attribute 'compat'。所以 TensorFlow 不再起作用了。

有人有稳定的可重复过程吗?我正在尝试使用 Deep Learning with Python, Second Edition.

中的代码

编辑 6:卸载 minconda 花了大约一个小时。安装新的 anaconda 效果很好。使用他们的 yaml 文件可以很好地安装。但是导入 tensorflow 会出现 dll 加载错误。

编辑 7:尝试 Python 3.8 并使用虚拟环境快得多。我可以安装 TensorFlow。但是使用它得到:

ImportError: Could not find the DLL(s) 'msvcp140_1.dll'. TensorFlow requires that these DLLs be installed in a directory that is named in your %PATH% environmen
t variable. 

我确实安装了可再发行组件并且 windows/system32 在路径上:

(handson-ml2) d:\ray\dev\mlbooks\handson-ml2>echo %PATH%
d:\pyvenvs\handson-ml2\Scripts;C:\Program Files (x86)\Common Files\Oracle\Java\j
avapath;c:\Rtools\bin;c:\Rtools\mingw_32\bin;C:\ProgramData\Oracle\Java\javapath
;C:\Program Files (x86)\AMD APP\bin\x86_64;C:\Program Files (x86)\AMD APP\bin\x8
6;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\Wi
ndowsPowerShell\v1.0\;C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Stati
c;C:\Program Files (x86)\Common Files\Acronis\SnapAPI\;C:\Program Files (x86)\AM
D\ATI.ACE\Core-Static;C:\gnuplot\bin;C:\Program Files\Git\cmd;C:\Program Files\C
alibre2\;C:\Program Files\MiKTeX 2.9\miktex\bin\x64\;C:\Program Files\MySQL\MySQ
L Utilities 1.6\;C:\Program Files (x86)\Common Files\Acronis\VirtualFile\;C:\Pro
gram Files (x86)\Common Files\Acronis\VirtualFile64\;C:\Program Files (x86)\Comm
on Files\Acronis\FileProtector\;C:\Program Files (x86)\Common Files\Acronis\File
Protector64\;C:\Program Files\nodejs\;C:\Program Files (x86)\Brackets\command;C:
\Program Files\Microsoft VS Code\bin;C:\Users\ray\AppData\Local\Programs\Python\
Launcher\;d:\bin;C:\Users\ray\AppData\Roaming\npm;C:\Users\ray\AppData\Local\ato
m\bin;C:\Program Files (x86)\FAHClient;x:\Program Files\Docker Toolbox

and here都是类似的问题。

建议将 dll 复制到 python 路径区域。

好像一夜之间变成了警告。现在我得到:

(handson-ml2) d:\ray\dev\mlbooks\handson-ml2>py
Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:37:02) [MSC v.1924 64 bit (AM
D64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
2020-06-25 13:53:13.746994: W tensorflow/stream_executor/platform/default/dso_lo
ader.cc:55] Could not load dynamic library 'cudart64_101.dll'; dlerror: cudart64
_101.dll not found
2020-06-25 13:53:13.776619: I tensorflow/stream_executor/cuda/cudart_stub.cc:29]
 Ignore above cudart dlerror if you do not have a GPU set up on your machine.
Pip subprocess error:
ERROR: Could not find a version that satisfies the requirement bokeh>=2.0.0 (fro
m panel>=0.7.0->holoviews->-r D:\ray\dev\COVID-19\condaenv.tvdi0rp1.requirements
.txt (line 5)) (from versions: 0.2, 0.3, 0.4, 0.4.1, 0.4.4, 0.5.0, 0.5.1, 0.5.2,
 0.6.0, 0.6.1, 0.7.0, 0.7.1, 0.8.0, 0.8.1, 0.8.2, 0.9.0, 0.9.1, 0.9.2, 0.9.3, 0.
10.0, 0.11.0, 0.11.1, 0.12.0, 0.12.1, 0.12.2, 0.12.3, 0.12.4, 0.12.5, 0.12.6, 0.
12.7, 0.12.9, 0.12.10, 0.12.11, 0.12.13, 0.12.14, 0.12.15, 0.12.16, 0.13.0, 1.0.
0, 1.0.1, 1.0.2, 1.0.3, 1.0.4, 1.1.0, 1.2.0, 1.3.0, 1.3.1, 1.3.2, 1.3.4, 1.4.0)
ERROR: No matching distribution found for bokeh>=2.0.0 (from panel>=0.7.0->holov
iews->-r D:\ray\dev\COVID-19\condaenv.tvdi0rp1.requirements.txt (line 5))


CondaEnvException: Pip failed

这是原始堆栈跟踪:

Traceback (most recent call last):
  File "d:\miniconda3\envs\tf2\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 58, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "d:\miniconda3\envs\tf2\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 28, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "d:\miniconda3\envs\tf2\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:\miniconda3\envs\tf2\lib\imp.py", line 242, in load_module
    return load_dynamic(name, filename, file)
  File "d:\miniconda3\envs\tf2\lib\imp.py", line 342, in load_dynamic
    return _load(spec)
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 "2.py", line 1, in <module>
    import tensorflow as tf
  File "d:\miniconda3\envs\tf2\lib\site-packages\tensorflow\__init__.py", line 41, in <module>
    from tensorflow.python.tools import module_util as _module_util
  File "d:\miniconda3\envs\tf2\lib\site-packages\tensorflow\python\__init__.py", line 50, in <module>
    from tensorflow.python import pywrap_tensorflow
  File "d:\miniconda3\envs\tf2\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 69, in <module>
    raise ImportError(msg)
ImportError: Traceback (most recent call last):
  File "d:\miniconda3\envs\tf2\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 58, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "d:\miniconda3\envs\tf2\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 28, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "d:\miniconda3\envs\tf2\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:\miniconda3\envs\tf2\lib\imp.py", line 242, in load_module
    return load_dynamic(name, filename, file)
  File "d:\miniconda3\envs\tf2\lib\imp.py", line 342, in load_dynamic
    return _load(spec)
ImportError: DLL load failed: The specified module could not be found.


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.

现状

我运行成一个类似的问题(this is the furthest i got) reproducibly on two machines. Some of the discussed issues seems to be known for example here: 1 2 3 4。 不仅要重现2, it makes sense to also start using virtual environments in order to test multiple tf versions. This can be achieved like this: (link for virtualenv on windows)

$ pip install --user pipenv

据我所知,有几件事可以解决。

检查版本和兼容性

your other question, it is very important to pay attention to respectively fitting versions. I also talked about this in that answer 也有评论。您以何种方式安装 python 版本和 anaconda?因为用户和系统模式选项都出现在 Windows (您可能正在处理) 上,这可能会产生干扰。要找出答案,您可以在 cmd 中键入这些命令:where anacondawhere python。这应该输出系统中已找到位置的列表。如果缺少某些内容,您还可以检查 PATH 以查看添加到那里的天气。

拥有相应的版本很重要 - 请参考 here or 作为起点。使用本段第一个 link 中的预制轮子可以使事情变得更容易,因为您只需要 select 正确的版本并安装它。第二个 link 讨论了 tensorflow 和 python 版本可能的不同组合。

当有多个不同版本(例如 python)时,另一个可能有用的提示是始终确保使用正确的版本。调用正确的 pip 来安装相应的 wheel 然后在 cmd 中看起来像这样:

C:\Users\XXXXX\AppData\Local\Programs\Python\Python38\Scripts\pip.exe install https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow_cpu-2.2.0-cp38-cp38-win_amd64.whl

而下一个命令在返回 pip 20.1.1 from C:\Users\XXXXX\.conda\envs\tf-cpu-AVX-env\lib\site-packages\pip (python 3.7) 后会在我的系统上出错(注意 python 版本 ):

pip.exe -V
pip.exe install https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow_cpu-2.2.0-cp38-cp38-win_amd64.whl

对于这些示例,将使用来自 google 的官方预制轮。请参阅 this site,其中介绍了一些最新的 tensorflow 软件包。请注意,此示例是既没有 GPU 也没有 AVX 的轮子。因为这样调用容易出错而且有些繁琐,所以建议使用开头提示的虚拟环境或者如下:

在虚拟环境中使用 (ana)conda

上面所说的虚拟环境不仅使它更方便,而且组织得更紧密。 (ana)conda 是一个很好的工具。 对于 anaconda gui,您可以在“environments”下创建一个新的 venv,并将您的 python 版本设置为例如 3.5。 然后,您可以通过环境列表旁边的包列表搜索 tensorflow 包并非常轻松地安装它。 您当然可能还必须安装其他软件包,例如Pandas、scikit-learn、numpy。

为了在命令行中使用 Conda,this link provides a good help for the first steps (also from your other question 上面提到的)。

关于此主题的进一步阅读(关于降级 tf):here

正在检查未满足的依赖关系

为了解决 Windows 系统上的“DLL 加载失败”问题,可以使用此 DLL dependency analyzer tool 检查依赖关系 这来自 link 4 from the beginning, leading to this post 关于如何处理 Windows 上的 DLL 加载失败问题的解释。 至此,我的系统上发现缺少这个动态库:api-ms-win-core-wow64-11-1-0.dll。

解决 Tensorflow CPU/GPU 问题

您遇到问题的一个可能原因是与 Tensorflow-GPU 不兼容。这是因为默认的 TF 包包含自 the TF 2.1 release.

以来的 CPU 和 GPU 版本

要检查这是否会导致您的某些问题,可以先尝试 CPU-only 变体。例如,您可以尝试从上面命名的 here (this is python 3.7 and tensorflow 2.0, decide weather to use AVX or not depending on the capabilities of your processor) or the google source 安装正确的 tensorflow-CPU wheel。


对于 Tensorflow-GPU,必须满足以下先决条件:

安装 NVIDIA cuDNN(用于深度神经网络的 GPU 加速基元库),例如cudnn-11.0-windows-x64-v8.0.1.13 这里。 注册 NVIDIA 开发者计划后,可以访问 here

请注意正确版本以获得兼容的 CUDA 安装 - 见上文! 当前的 Tensorflow(2.2.0,但对于 2020 年 7 月 23 日的 TF ≥ 2.1.0 也是如此)需要

  • CUDA 10.1 and the respective cuDNN ⤷ 这仅在 2019 年 11 月 5 日的 cuDNNv7.6.5 中完全匹配,afaik。
  • 对于 Cuda 10.1,需要 418.x 或更高版本的 NVIDIA® GPU 驱动程序。这可以访问,例如here but has to be picked according to your platform. (Pay attention to not use a DCH driver where not applicable, see here.)
  • CUDA 工具包附带的 CUPTI。
  • cuDNN SDK (>= 7.6)
  • (可选)TensorRT 6.0 以改善某些模型推理的延迟和吞吐量。

(部分摘自here

为了安装 cuDNN(见上文),必须下载正确的存档,解压缩并从其目录复制到相应的 CUDA x.y 安装位置。对于 windows,三个文件的位置是:(其中 x.y 与您的特定安装相匹配,例如 10.2 - 更多信息 here and here

C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\vx.y\bin
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\vx.y\include
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\vx.y\lib

实际上,不使用确切的 CUDA 版本 10.1(而是例如 10.2)会导致错误,例如:W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cudart64_101.dll'; dlerror: cudart64_101.dll not found

要在 AVX 或 GPU 可用性上测试您的 tensorflow 配置,您可以使用

在您的 GPU 上使用 Tensorflow

我注意到,您正在使用两个 AMD GPU。我不知道带有 OpenGL 的端口或类似的东西,因为它总是谈论 CUDA 计算能力(reference - 对于 tensorflow 应该≥3.5,并且仅适用于 NVIDIA GPU)

请参阅 (或可能还有其他)以获取与缺少 CUDA 支持相关的问题的解决方案。

进一步阅读有关为 Tensorflow 启用 GPU 的信息:here

如果以上所有方法都没有帮助,另一种可能的方法是 build tensorflow from source 自己(这也可以提高性能)。