如何修复-ImportError:导入keras时,DLL加载失败

时间:2019-02-09 18:49:17

标签: python windows tensorflow keras

我正在尝试使用tensorflow后端安装keras

我先运行pip install keras,然后运行pip install tensorflow 这两个命令都成功完成,现在当我尝试从keras.models导入Sequential时出现错误

这是我的代码

code

这是错误

Using TensorFlow backend.
Traceback (most recent call last):
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 58, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 28, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python36\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 "C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\imp.py", line 242, in load_module
    return load_dynamic(name, filename, file)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\imp.py", line 342, in load_dynamic
    return _load(spec)
ImportError: DLL load failed: Произошел сбой в программе инициализации библиотеки динамической компоновки (DLL).

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/Admin/PycharmProjects/keras/test.py", line 3, in <module>
    from keras.models import Sequential
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\__init__.py", line 3, in <module>
    from . import utils
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\utils\__init__.py", line 6, in <module>
    from . import conv_utils
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\utils\conv_utils.py", line 9, in <module>
    from .. import backend as K
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\backend\__init__.py", line 89, in <module>
    from .tensorflow_backend import *
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\keras\backend\tensorflow_backend.py", line 5, in <module>
    import tensorflow as tf
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\__init__.py", line 24, in <module>
    from tensorflow.python import pywrap_tensorflow  # pylint: disable=unused-import
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\__init__.py", line 49, in <module>
    from tensorflow.python import pywrap_tensorflow
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 74, in <module>
    raise ImportError(msg)
ImportError: Traceback (most recent call last):
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 58, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 28, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python36\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 "C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\imp.py", line 242, in load_module
    return load_dynamic(name, filename, file)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python36\lib\imp.py", line 342, in load_dynamic
    return _load(spec)
ImportError: DLL load failed: Произошел сбой в программе инициализации библиотеки динамической компоновки (DLL).


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.

我不知道为什么错误是俄语的,也许是因为我的系统语言,但这是翻译

Original error:
ImportError: DLL load failed: Произошел сбой в программе инициализации библиотеки динамической компоновки (DLL)

Translation:
ImportError: DLL load failed: A crash occurred in the dynamic link library initialization program. (DLL)

我正在使用

versions

和Python 3.6.0

有人可以帮我解决这个问题吗?

5 个答案:

答案 0 :(得分:0)

方法1

因此,根据您提供的信息,请按照以下步骤操作:

  1. 安装Visual Studio C++ Redistributable 2015 Update 3
  2. 由于您的CPU不支持AVX,请转到此repository。它包含支持SSE的TensorFlow构建,该构建与Intel Pentium兼容。
  3. 在回购中遵循此路径-> tensorflow-windows-wheel/1.12.0/py36/CPU/sse2
  4. 下载.whl文件(或单击here)。
  5. 现在,我们需要安装此文件。使用

    pip install tensorflow-1.12.0-cp36-cp36m-win_amd64.whl

  6. 如果显示任何错误,请尝试将相对路径更改为绝对路径。

  7. TensorFlow已成功安装。

方法2

安装Anaconda。它使用名为conda的程序包管理器,类似于pip

  1. 创建一个新的Conda环境。

    conda create --name mytfenv

  2. 安装TensorFlow(version = 1.10.0)

    conda install tensorflow

其中一种方法将为您服务。我个人遵循这两种方法,因为我的CPU规格符合您的要求。

答案 1 :(得分:0)

Tensorflow 1.12与Keras(tf.kerashttps://www.tensorflow.org/guide/keras)一起提供。因此,您必须先导入tensorflow和keras,然后使用keras.Sequential创建图层。

import tensorflow as tf
from tensorflow import keras

#for example
model = keras.Sequential([
    keras.layers.Flatten(input_shape=(28, 28)),
    keras.layers.Dense(128, activation=tf.nn.relu),
    keras.layers.Dense(10, activation=tf.nn.softmax)
])

有关更多信息,请参阅官方教程:https://www.tensorflow.org/tutorials/keras/basic_classification

答案 2 :(得分:0)

对于CPU,您可以通过在jupyter笔记本中运行以下python代码来简单地安装tensorflow。我希望你安装了水蟒!

          pip.main(['install','tensorflow==1.5'])

tensorflow版本大于1.6可能会给您带来问题,但它具有一些有用的功能

答案 3 :(得分:0)

我在Windows 10上使用python 3.7,Jypyter Notebook遇到相同的问题,然后执行以下操作:

  1. 安装The latest supported Visual C++ downloads- Visual Studio 2015、2017和2019 。重新启动。
  2. 然后在我的Jypyter Notebook中的tensorflow website上进行安装指南:

    • pip install --upgrade pip

    • pip install tensorflow

  3. 然后我的命令import tensorflowimport keras正常工作。

答案 4 :(得分:0)

您可以安装Anaconda,创建虚拟环境,在其上安装Keras和Tensorflow,如果在jupyter笔记本中仍出现DLL导入错误- https://support.microsoft.com/en-us/help/2977003/the-latest-supported-visual-c-downloads 打开链接,下载x64 .exe文件并安装它。