Modulenotfounderror: No Module Named 'Tf'
I'm Having Problem with Tensorflow. I Want to Use Imagedatagenerator, but I'm Receiving Error Modulenotfounderror: No Module Named 'Tf'. Not Sure What Is the...
I'm having problem with tensorflow. I want to use ImageDataGenerator, but I'm receiving error ModuleNotFoundError: No module named 'tf'. Not sure what is the problem. I added this tf.version to test will it work, and it shows the version of tensorflow.
import tensorflow as tf
from tensorflow import keras
print(tf.__version__)
from tf.keras.preprocessing.image import ImageDataGenerator
When I run this code, I get this:
2.1.0
Traceback (most recent call last):
File "q:/TF/Kamen papir maaze/rks.py", line 14, in <module>
from tf.keras.preprocessing.image import ImageDataGenerator
ModuleNotFoundError: No module named 'tf'
3 Answers
The line
import tensorflow as tf
means you are importing tensorflow with an alias as tf to call it modules/functions.
You cannot use the alias to import other modules.
For your case, if you call directly
tf.keras.preprocessing.image.ImageDataGenerator(...)
then it will work.
or
you need to import the module with the right module name. i.e.
from tensorflow.keras.preprocessing.image import ImageDataGenerator
This works, tested on kaggle with tf_v2.6 and tf_v2.7
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.models import Sequential
...
in Tensorflow 2.0+, to use keras instead of tf use tensorflow always-
import tensorflow
from tensorflow.keras.preprocessing.image import ImageDataGenerator