Andranik
Andranik

Reputation: 156

How to fix - ImportError: DLL load failed, when importing keras

I am trying to install keras with tensorflow backend

I have run pip install keras at first, and then pip install tensorflow both commands finished succesfully, now when i'm trying to import Sequential from keras.models I get error

Here is my code

code

Here is error

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.

I don't know why error is in Russian, maybe it is because of my system language, but here is translation

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

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

I am using

versions

And Python 3.6.0

Can someone help me to solve this issue?

Upvotes: 2

Views: 8706

Answers (5)

fin
fin

Reputation: 107

You can install Anaconda, create virtual env, install Keras and Tensorflow on it and if still getting DLL Import Error in jupyter notebook - https://support.microsoft.com/en-us/help/2977003/the-latest-supported-visual-c-downloads Open up the link, Download the x64 .exe file and install it.

Upvotes: 0

Jackssn
Jackssn

Reputation: 1614

I have same problem with Python 3.7, Jypyter Notebook on Windows 10. And do this:

  1. Install The latest supported Visual C++ downloads - Visual Studio 2015, 2017 and 2019. With reboot.
  2. And do install-guide on tensorflow website in my Jypyter Notebook:

    • pip install --upgrade pip

    • pip install tensorflow

  3. Then my commands import tensorflow or import keras work correctly.

Upvotes: 0

Prosper Makotore
Prosper Makotore

Reputation: 1

for CPU you can simply install tensorflow by running the following python code in jupyter notebook. i hope you anaconda installed!

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

a tensorflow of version greater than 1.6 might give you problems but it comes with some useful features

Upvotes: 0

iv67
iv67

Reputation: 4151

Tensorflow 1.12 comes together with Keras (tf.keras : https://www.tensorflow.org/guide/keras). So you have to import tensorflow and keras first, then use the keras.Sequential to create the layer.

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)
])

For more information refer to official tutorial : https://www.tensorflow.org/tutorials/keras/basic_classification

Upvotes: 0

Shubham Panchal
Shubham Panchal

Reputation: 4299

Approach 1

So, according to the information you provided, follow these steps :

  1. Install Visual Studio C++ Redistributable 2015 Update 3.
  2. Since your CPU does not support AVX, go to this repository. It contains TensorFlow builds supporting SSE, which is compatible with Intel Pentium.
  3. Follow this path in the repo -> tensorflow-windows-wheel/1.12.0/py36/CPU/sse2
  4. Download the .whl file ( or click here ).
  5. Now, we need to install this file. Use,

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

  6. If it shows any error, try changing the relative path to an absolute one.

  7. TensorFlow is installed successfully.

Approach 2

Install Anaconda. It uses a package manager named conda which is similar to pip.

  1. Create a new Conda environment.

    conda create --name mytfenv

  2. Install TensorFlow ( version=1.10.0 )

    conda install tensorflow

One of the methods would work for you. I personally followed both the approachs as my CPU specifications match yours.

Upvotes: 3

Related Questions