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

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
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Robert Thorne

Robert Thorne

Automotive & Future Transportation Editor

Robert Thorne covers electric vehicle innovations, autonomous driving systems, global mobility trends, and automotive engineering developments.

Share this article
Twitter Facebook Pinterest