No module named 'keras'

Hi,
I am doing “deep learning for robotics”. In unit4, the exercise is to integrate the given codes into a main.py to implement process of CNN. But actually the solution doesn’t work and show errors like this:

2022-12-22 15:21:07.356609: I tensorflow/compiler/jit/xla_cpu_device.cc:41] Not creating XLA devices, tf_xla_enable_xla_devices not set
2022-12-22 15:21:07.401055: W tensorflow/stream_executor/platform/default/dso_loader.cc:60] Could not load dynamic library 'libcuda.so.1'; dlerror: libcuda.so.1: cannot openshared object file: No such file or directory; LD_LIBRARY_PATH: /home/user/catkin_ws/devel/lib:/home/simulations/public_sim_ws/devel/lib:/opt/ros/noetic/lib:/opt/ros/noetic/lib/x86_64-linux-gnu:/usr/local/cuda-10.2/lib64:/home/user/.catkin_ws_python3/src/TensorRT-7.0.0.11/lib:/usr/local/cuda-10.2/lib64:/home/user/.catkin_ws_python3/src/TensorRT-7.0.0.11/lib:/usr/local/nvidia/lib:/usr/local/nvidia/lib64:/usr/lib/x86_64-linux-gnu/gazebo-11/plugins:/usr/lib/x86_64-linux-gnu/gazebo-11/plugins
2022-12-22 15:21:07.401707: W tensorflow/stream_executor/cuda/cuda_driver.cc:326] failed call to cuInit: UNKNOWN ERROR (303)
2022-12-22 15:21:07.402863: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:156] kernel driver does not appear to be running on this host (4_xterm): /proc/driver/nvidia/version does not exist
2022-12-22 15:21:07.407000: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2 FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2022-12-22 15:21:07.413290: I tensorflow/compiler/jit/xla_gpu_device.cc:99] Not creating XLA devices, tf_xla_enable_xla_devices not set
2022-12-22 15:21:08.168443: W tensorflow/core/framework/cpu_allocator_impl.cc:80] Allocation of 491520000 exceeds 10% of free system memory.
Killed

Unfortunately I cannot understand this error information.

By the way, there is an obvious problem about Keras. When we import on our own PC compilers like:

import keras
import tensorflow as tf

everything goes normally.
But when we import like:

import tensorflow.keras as keras

the CMD will tell that no module named 'keras'. This has been a universal bug about keras.
However, in webshell of TheConstruct, import tensorflow.keras works fine. Can anyone inform me of how TheConstruct achieve this correction? I have tried some methods from Google to solve this bug on my own Vscode, but nothing works. It seem TheConstruct has already solved this…

Hi @MeineLiebeAxt ,

Regarding the no module found 'keras' error when you import tensorflow.keras as keras is because of older version.

Doing a quick Google search, I got this link: ModuleNotFoundError: No module named 'keras' when using tensorflow 2.6 - Stack Overflow

So, basically, if you downgrade tensorflow version to 2.5, you will be able to use the keras package that comes along. However, with tensorflow version 2.6 and above, keras does not seem to be a part of main tensorflow package.

I hope this solves one of your problems.

To know what version of tensorflow is installed on TheConstruct page, do the following:

Method 1: Run python3 on a terminal and write out the following commands.

import tensorflow as tf
print(tf.__version__)

Method 2: use pip list or pip freeze on the terminal
pip list | grep tensorflow
(OR)
pip freeze | grep tensorflow

Regards,
Girish

Hi @girishkumar.kannan ,
thank you for you explanation. I have just begun tensorflow learning recently , so I got the newest version 2.11.0. No wonder I got this problem …

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.