【问题标题】:Installing Tensorflow 2 gets a dll failed to load in pywrap_tensorflow.py安装 Tensorflow 2 导致 dll 无法加载到 pywrap_tensorflow.py
【发布时间】:2020-06-14 18:31:59
【问题描述】:

在过去一年左右的时间里,我有 problemshereherethere 安装了 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),但它产生了 same 错误。

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

line 28: _pywrap_tensorflow_internal = swig_import_helper()

在 swig 助手中:

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

看起来它正在寻找自己? 有人知道吗?

编辑 3:This 看起来像同样的问题,但它已经过时了。 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 "Failed to load the native TensorFlow runtime" 结果大约有 3.6k 次点击,所以这是一个持续存在的问题。

编辑 5:安装 Keras 会出现 this 错误。尝试修复得到:AttributeError: module 'tensorflow' has no attribute 'compat'。所以TensorFlow不再工作了。

有没有人有一个稳定的可重复过程?我正在尝试使用来自Deep Learning with Python, Second Edition 的代码。

编辑 6:卸载 minconda 大约需要一个小时。安装新的蟒蛇工作正常。使用他们的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

Herehere 是类似的问题。

Some 建议将 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.

【问题讨论】:

    标签: python-3.x dll installation tensorflow2.0 miniconda


    【解决方案1】:

    现状

    我在两台机器上重复遇到了类似的问题(this 是我得到的最远的问题)。例如,这里讨论的一些问题似乎是已知的:1234。 不仅要重现2,还需要开始使用虚拟环境来测试多个 tf 版本。这可以这样实现:(link for virtualenv on windows)

    $ pip install --user pipenv
    

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

    检查版本和兼容性

    正如your other question 上的评论一样,注意分别适合的版本非常重要。我也在that answer 中谈到了这个问题。您以哪种方式安装了 python 版本和 anaconda?因为用户和系统模式选项都存在于 Windows (您可能正在使用) 上,这可能会产生干扰。要找出答案,您可以在 cmd 中键入以下命令:where anacondawhere python。这应该输出系统中找到的位置列表。如果缺少某些内容,您还可以查看 PATH 以查看在此处添加的天气。

    拥有相应的版本很重要——参考herehere 2 作为起点。使用本段第一个链接中的预建轮子使事情变得更容易,因为您只需选择正确的版本并安装它。第二个链接讨论了可能的 tensorflow 和 python 版本的不同组合。

    当拥有多个不同版本(例如 python)时,另一个可能会有所帮助的提示是始终确保使用正确的版本。调用正确的 pip 来安装相应的轮子,然后在 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,您可以在“环境”下创建一个新的 venv,并将您的 python 版本设置为例如 3.5。 然后你可以通过环境列表旁边的包列表搜索tensorflow包并非常容易地安装它。 当然,您可能还必须安装其他软件包,例如Pandas、scikit-learn、numpy。

    对于在命令行中使用 Conda,this link 为第一步提供了很好的帮助(也来自上面提到的your other question)。

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

    检查未满足的依赖项

    为了解决Windows系统上的“DLL加载失败”问题,可以使用DLL dependency analyzer tool检查依赖关系 这来自链接4 从一开始,导致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 的变体。例如,您可以尝试从here(这是 python 3.7 和 tensorflow 2.0,根据您的处理器的能力决定使用 AVX 的天气)或上面提到的the google source 安装正确的 tensorflow-CPU 轮。


    对于 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.1respective cuDNN ⤷ 这仅在 2019 年 11 月 5 日的 cuDNNv7.6.5 中完全匹配,afaik。
    • 对于 Cuda 10.1,需要 418.x 或更高版本的合适 NVIDIA® GPU 驱动程序。这可以访问,例如here 但必须是 picked according to your platform。 (注意不要在不适用的情况下使用 DCH 驱动程序,请参阅here。)
    • CUPTI,随 CUDA 工具包一起提供。
    • cuDNN SDK (>= 7.6)
    • (可选)TensorRT 6.0 可改善某些模型的推理延迟和吞吐量。

    (部分摘自here

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

    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 配置,您可以使用this approach

    在你的 GPU 上使用 Tensorflow

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

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

    为 Tensorflow 启用 GPU 的进一步阅读:here

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

    【讨论】:

    • 我下载了一个最近的 python,然后在没有 anaconda 的情况下创建了一个虚拟环境。也快得多,蟒蛇是一只真正的狗。感谢您的详尽回答。
    猜你喜欢
    • 2018-12-26
    • 2020-05-06
    • 1970-01-01
    • 1970-01-01
    • 2021-07-19
    • 1970-01-01
    • 1970-01-01
    • 2016-04-27
    相关资源
    最近更新 更多