Error while importing keras and tensorflow

Viewed 12276

I used from keras.layers import dot and got this error in return:

c:\users\prashasti\appdata\local\programs\python\python37-32\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py in swig_import_helper()
     17         try:
---> 18             fp, pathname, description = imp.find_module('_pywrap_tensorflow_internal', [dirname(__file__)])
     19         except ImportError:

c:\users\prashasti\appdata\local\programs\python\python37-32\lib\imp.py in find_module(name, path)
    295     else:
--> 296         raise ImportError(_ERR_MSG.format(name), name=name)
    297 

ImportError: No module named '_pywrap_tensorflow_internal'

During handling of the above exception, another exception occurred:

ModuleNotFoundError                       Traceback (most recent call last)
c:\users\prashasti\appdata\local\programs\python\python37-32\lib\site-packages\tensorflow\python\pywrap_tensorflow.py in <module>
     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\prashasti\appdata\local\programs\python\python37-32\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py in <module>
     27             return _mod
---> 28     _pywrap_tensorflow_internal = swig_import_helper()
     29     del swig_import_helper

c:\users\prashasti\appdata\local\programs\python\python37-32\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py in swig_import_helper()
     19         except ImportError:
---> 20             import _pywrap_tensorflow_internal
     21             return _pywrap_tensorflow_internal

ModuleNotFoundError: No module named '_pywrap_tensorflow_internal'

During handling of the above exception, another exception occurred:

ImportError                               Traceback (most recent call last)
c:\users\prashasti\appdata\local\programs\python\python37-32\lib\site-packages\keras\__init__.py in <module>
      2 try:
----> 3     from tensorflow.keras.layers.experimental.preprocessing import RandomRotation
      4 except ImportError:

c:\users\prashasti\appdata\local\programs\python\python37-32\lib\site-packages\tensorflow\__init__.py in <module>
     23 # pylint: disable=wildcard-import
---> 24 from tensorflow.python import *
     25 # pylint: enable=wildcard-import

c:\users\prashasti\appdata\local\programs\python\python37-32\lib\site-packages\tensorflow\python\__init__.py in <module>
     48 
---> 49 from tensorflow.python import pywrap_tensorflow
     50 

c:\users\prashasti\appdata\local\programs\python\python37-32\lib\site-packages\tensorflow\python\pywrap_tensorflow.py in <module>
     51 above this error message when asking for help.""" % traceback.format_exc()
---> 52   raise ImportError(msg)
     53 

ImportError: Traceback (most recent call last):
  File "c:\users\prashasti\appdata\local\programs\python\python37-32\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 18, in swig_import_helper
    fp, pathname, description = imp.find_module('_pywrap_tensorflow_internal', [dirname(__file__)])
  File "c:\users\prashasti\appdata\local\programs\python\python37-32\lib\imp.py", line 296, in find_module
    raise ImportError(_ERR_MSG.format(name), name=name)
ImportError: No module named '_pywrap_tensorflow_internal'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\users\prashasti\appdata\local\programs\python\python37-32\lib\site-packages\tensorflow\python\pywrap_tensorflow.py", line 41, in <module>
    from tensorflow.python.pywrap_tensorflow_internal import *
  File "c:\users\prashasti\appdata\local\programs\python\python37-32\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 28, in <module>
    _pywrap_tensorflow_internal = swig_import_helper()
  File "c:\users\prashasti\appdata\local\programs\python\python37-32\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 20, in swig_import_helper
    import _pywrap_tensorflow_internal
ModuleNotFoundError: 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.

During handling of the above exception, another exception occurred:

ImportError                               Traceback (most recent call last)
<ipython-input-14-884a93be26a0> in <module>
----> 1 from keras.layers import dot

c:\users\prashasti\appdata\local\programs\python\python37-32\lib\site-packages\keras\__init__.py in <module>
      4 except ImportError:
      5     raise ImportError(
----> 6         'Keras requires TensorFlow 2.2 or higher. '
      7         'Install TensorFlow via `pip install tensorflow`')
      8 

ImportError: Keras requires TensorFlow 2.2 or higher. Install TensorFlow via `pip install tensorflow`

I upgraded tensorflow. reinstalled it too but didn't work.

2 Answers

I have CUDA 10.0 and cuDNN 7.6.4 I also getting this below error

ImportError: Keras requires TensorFlow 2.2 or higher. Install TensorFlow via pip install tensorflow

What I did was first uninstall everything like no python, no anaconda, no virtual env.

Then perform below steps:

  1. Install anaconda win 64 with python 3.7 as one package
  2. After Anaconda install, need to set the path C:\Users\username\Anaconda3\Scripts
  3. update pip
  4. Conda update
  5. conda create -n tf20 python=3.7
  6. pip install numpy
  7. pip install "tensorflow>=1.15,<2.0"
  8. pip install "tensorflow-gpu>=1.15,<2.0" (If GPU support needed)
  9. pip install keras (installed with latest 2.4.2)
  10. pip install --upgrade tensorflow-hub
  11. pip install keras==2.2.4 (downgraded to 2.2.4)

Now keras running with tensorflow 1.15 Probably you can do pip install keras==2.2.4 in step 9 and remove step 11. Just try once above steps from step 2 onwards before uninstall everthing. If that works that you can save your time. Good Luck :)

Please do let me know if this worked for you.

-Thanks

Hey More then one solution for same problem -->

  • Solution -> 1

    1. Check environment settings of CUDA, normally all the settings of CUDA had been added to Windows environment

    2. Copy files in bin, lib and include of cuDNN to bin, lib and include of CUDA respectively. Normally the directory is C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA

    3. And then you can import tensorflow and run your code. Good luck!

  • Solution -> 2

    1. Just run below command if necessary then update it

    2. pip install tensorflow==1.5

Related