(solved) Python can not import keras

Questions about applications and software
Forum rules
Before you post read how to get help. Topics in this forum are automatically closed 6 months after creation.
Locked
deepakdeshp
Level 20
Level 20
Posts: 12334
Joined: Sun Aug 09, 2015 10:00 am

(solved) Python can not import keras

Post by deepakdeshp »

Hello,
I installed keras yet I cant import it. What am I missing?

Code: Select all

 pip3 show keras
Name: Keras
Version: 2.4.3
Summary: Deep Learning for humans
Home-page: https://github.com/keras-team/keras
Author: Francois Chollet
Author-email: francois.chollet@gmail.com
License: MIT
Location: /home/uma/anaconda3/lib/python3.7/site-packages
Requires: h5py, scipy, numpy, pyyaml
Required-by: 
(base) uma@uma ~ $ python3.9
Python 3.9.5 (default, May 19 2021, 11:32:47) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow
>>> import keras
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'keras'  
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 3 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
If I have helped you solve a problem, please add [SOLVED] to your first post title, it helps other users looking for help.
Regards,
Deepak

Mint 21.1 Cinnamon 64 bit with AMD A6 / 8GB
Mint 21.1 Cinnamon AMD Ryzen3500U/8gb
User avatar
spamegg
Level 14
Level 14
Posts: 5031
Joined: Mon Oct 28, 2019 2:34 am
Contact:

Re: Python can not import keras

Post by spamegg »

Code: Select all

import numpy as np
import tensorflow as tf
from tensorflow import keras
https://keras.io/getting_started/intro_ ... engineers/
deepakdeshp
Level 20
Level 20
Posts: 12334
Joined: Sun Aug 09, 2015 10:00 am

Re: Python can not import keras

Post by deepakdeshp »

Code: Select all

  from tensorflow import keras
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name 'keras' from 'tensorflow' (unknown location)
 
If I have helped you solve a problem, please add [SOLVED] to your first post title, it helps other users looking for help.
Regards,
Deepak

Mint 21.1 Cinnamon 64 bit with AMD A6 / 8GB
Mint 21.1 Cinnamon AMD Ryzen3500U/8gb
deepakdeshp
Level 20
Level 20
Posts: 12334
Joined: Sun Aug 09, 2015 10:00 am

Re: Python can not import keras

Post by deepakdeshp »

Code: Select all

pip3 show numpy
Name: numpy
Version: 1.20.3
Summary: NumPy is the fundamental package for array computing with Python.
Home-page: https://www.numpy.org
Author: Travis E. Oliphant et al.
Author-email: None
License: BSD
Location: /home/uma/anaconda3/lib/python3.7/site-packages
Requires: 
Required-by: tensorflow, tensorboard, tables, statsmodels, seaborn, scipy, scikit-learn, PyWavelets, pytest-arraydiff, patsy, pandas, opt-einsum, opencv-python, numexpr, numba, mkl-random, mkl-fft, matplotlib, Keras, Keras-Preprocessing, imageio, h5py, Bottleneck, bokeh, bkcharts, astropy
(base) uma@uma ~ $ python3.9
Python 3.9.5 (default, May 19 2021, 11:32:47) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/numpy/core/__init__.py", line 17, in <module>
    from . import multiarray
  File "/usr/lib/python3/dist-packages/numpy/core/multiarray.py", line 14, in <module>
    from . import overrides
  File "/usr/lib/python3/dist-packages/numpy/core/overrides.py", line 7, in <module>
    from numpy.core._multiarray_umath import (
ModuleNotFoundError: No module named 'numpy.core._multiarray_umath'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3/dist-packages/numpy/__init__.py", line 142, in <module>
    from . import core
  File "/usr/lib/python3/dist-packages/numpy/core/__init__.py", line 47, in <module>
    raise ImportError(msg)
ImportError: 

IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!

Importing the numpy c-extensions failed.
- Try uninstalling and reinstalling numpy.
- If you have already done that, then:
  1. Check that you expected to use Python3.9 from "/usr/bin/python3.9",
     and that you have no directories in your PATH or PYTHONPATH that can
     interfere with the Python and numpy version "1.17.4" you're trying to use.
  2. If (1) looks fine, you can open a new issue at
     https://github.com/numpy/numpy/issues.  Please include details on:
     - how you installed Python
     - how you installed numpy
     - your operating system
     - whether or not you have multiple versions of Python installed
     - if you built from source, your compiler versions and ideally a build log

- If you're working with a numpy git repository, try `git clean -xdf`
  (removes all files not under version control) and rebuild numpy.

Note: this error has many possible causes, so please don't comment on
an existing issue about this - open a new one instead.

Original error was: No module named 'numpy.core._multiarray_umath'

If I have helped you solve a problem, please add [SOLVED] to your first post title, it helps other users looking for help.
Regards,
Deepak

Mint 21.1 Cinnamon 64 bit with AMD A6 / 8GB
Mint 21.1 Cinnamon AMD Ryzen3500U/8gb
User avatar
spamegg
Level 14
Level 14
Posts: 5031
Joined: Mon Oct 28, 2019 2:34 am
Contact:

Re: Python can not import keras

Post by spamegg »

You are using Python 3.9 but the pip3 packages might have been installed for Python 3.8 (system) so there might be a confusion there. Not sure.

I would recommend removing Python 3.9 completely, and installing numpy, tensorflow, keras for the system Python.

If you want different Python versions then use pyenv https://github.com/pyenv/pyenv/
deepakdeshp
Level 20
Level 20
Posts: 12334
Joined: Sun Aug 09, 2015 10:00 am

Re: Python can not import keras

Post by deepakdeshp »

spamegg wrote: Wed Jun 16, 2021 11:40 am You are using Python 3.9 but the pip3 packages might have been installed for Python 3.8 (system) so there might be a confusion there. Not sure.

I would recommend removing Python 3.9 completely, and installing numpy, tensorflow, keras for the system Python.

If you want different Python versions then use pyenv https://github.com/pyenv/pyenv/
Thanks. I am able to import keras and numpy with python3.7. But I am not sure how to set the GPU to use tensorflow.

Code: Select all

  python3.7
Python 3.7.4 (default, Aug 13 2019, 20:35:49) 
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow
2021-06-16 21:34:12.608970: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcudart.so.11.0'; dlerror: libcudart.so.11.0: cannot open shared object file: No such file or directory
2021-06-16 21:34:12.609032: 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.
>>> from tensorflow import keras
>>> import numpy
>>> 
KeyboardInterrupt
>>> 
(base) uma@uma ~ $ inxi -G
Graphics:
  Device-1: AMD Picasso driver: amdgpu v: kernel 
  Display: x11 server: X.Org 1.20.9 driver: modesetting unloaded: fbdev,vesa 
  resolution: 1920x1080~60Hz 
  OpenGL: renderer: AMD RAVEN (DRM 3.40.0 5.11.0-20-generic LLVM 10.0.0) 
  v: 4.6 Mesa 20.0.0-devel - padoka PPA 
If I have helped you solve a problem, please add [SOLVED] to your first post title, it helps other users looking for help.
Regards,
Deepak

Mint 21.1 Cinnamon 64 bit with AMD A6 / 8GB
Mint 21.1 Cinnamon AMD Ryzen3500U/8gb
deepakdeshp
Level 20
Level 20
Posts: 12334
Joined: Sun Aug 09, 2015 10:00 am

Re: Python can not import keras

Post by deepakdeshp »

Code: Select all

python3.7
Python 3.7.4 (default, Aug 13 2019, 20:35:49) 
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
2021-06-16 21:39:56.561764: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcudart.so.11.0'; dlerror: libcudart.so.11.0: cannot open shared object file: No such file or directory
2021-06-16 21:39:56.561816: 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.
>>> print("Num GPUs Available: ", len(tf.config.list_physical_devices('GPU')))
2021-06-16 21:40:02.817477: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcuda.so.1'; dlerror: libcuda.so.1: cannot open shared object file: No such file or directory
2021-06-16 21:40:02.817550: W tensorflow/stream_executor/cuda/cuda_driver.cc:326] failed call to cuInit: UNKNOWN ERROR (303)
2021-06-16 21:40:02.817594: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:156] kernel driver does not appear to be running on this host (uma): /proc/driver/nvidia/version does not exist
Num GPUs Available:  0
  
If I have helped you solve a problem, please add [SOLVED] to your first post title, it helps other users looking for help.
Regards,
Deepak

Mint 21.1 Cinnamon 64 bit with AMD A6 / 8GB
Mint 21.1 Cinnamon AMD Ryzen3500U/8gb
User avatar
spamegg
Level 14
Level 14
Posts: 5031
Joined: Mon Oct 28, 2019 2:34 am
Contact:

Re: Python can not import keras

Post by spamegg »

You have AMD Graphics, I think you need an Nvidia graphics card for that.

Code: Select all

2021-06-16 21:40:02.817550: W tensorflow/stream_executor/cuda/cuda_driver.cc:326] failed call to cuInit: UNKNOWN ERROR (303)
2021-06-16 21:40:02.817594: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:156] kernel driver does not appear to be running on this host (uma): /proc/driver/nvidia/version does not exist
CUDA is Nvidia's own GPU computation framework.

Unfortunately most machine learning/GPU-compute libraries do not support AMD. You might try PlaidML: https://github.com/plaidml/plaidml
deepakdeshp
Level 20
Level 20
Posts: 12334
Joined: Sun Aug 09, 2015 10:00 am

Re: Python can not import keras

Post by deepakdeshp »

Is there a way to install tensorflow and numpy for python3.9?
If I have helped you solve a problem, please add [SOLVED] to your first post title, it helps other users looking for help.
Regards,
Deepak

Mint 21.1 Cinnamon 64 bit with AMD A6 / 8GB
Mint 21.1 Cinnamon AMD Ryzen3500U/8gb
User avatar
spamegg
Level 14
Level 14
Posts: 5031
Joined: Mon Oct 28, 2019 2:34 am
Contact:

Re: Python can not import keras

Post by spamegg »

Is there a way to install tensorflow and numpy for python3.9?
Use pyenv. Won't make a difference though, you still don't have Nvidia.
deepakdeshp
Level 20
Level 20
Posts: 12334
Joined: Sun Aug 09, 2015 10:00 am

Re: Python can not import keras

Post by deepakdeshp »

spamegg wrote: Wed Jun 16, 2021 11:58 pm
Is there a way to install tensorflow and numpy for python3.9?
Use pyenv. Won't make a difference though, you still don't have Nvidia.
I am not talking about the display here. Now tensorflow and keras runs under python3.7 and gives error in python3.9 Is there a way to run them under python3.9?
If I have helped you solve a problem, please add [SOLVED] to your first post title, it helps other users looking for help.
Regards,
Deepak

Mint 21.1 Cinnamon 64 bit with AMD A6 / 8GB
Mint 21.1 Cinnamon AMD Ryzen3500U/8gb
User avatar
spamegg
Level 14
Level 14
Posts: 5031
Joined: Mon Oct 28, 2019 2:34 am
Contact:

Re: Python can not import keras

Post by spamegg »

Yes, with pyenv it should be possible. You remove your 3.9 (which I assume you got from deadsnakes-ppa or built from source manually), install pyenv, then install 3.9 using pyenv, then activate that 3.9 using pyenv, then install those packages again, which will get installed for that 3.9 inside pyenv's virtual environment separate from the system Python. It will be installed to a folder like ~/.pyenv/versions/3.9.5/lib/python3.9
deepakdeshp
Level 20
Level 20
Posts: 12334
Joined: Sun Aug 09, 2015 10:00 am

Re: Python can not import keras

Post by deepakdeshp »

Thank you spamegg for your patience.
If I have helped you solve a problem, please add [SOLVED] to your first post title, it helps other users looking for help.
Regards,
Deepak

Mint 21.1 Cinnamon 64 bit with AMD A6 / 8GB
Mint 21.1 Cinnamon AMD Ryzen3500U/8gb
tkocou
Level 2
Level 2
Posts: 82
Joined: Mon Jul 30, 2012 6:25 pm

Re: (solved) Python can not import keras

Post by tkocou »

For someone wanting to use AMD GPUs with tensorflow, this article may help:
https://medium.com/nerd-for-tech/use-yo ... 7e34ff3554
Linux Mint 21.1 Cinnamon
Locked

Return to “Software & Applications”