0

Why do I get a ModuleNotFoundError: No module named 'tf' for:

import tensorflow as tf
import tf.keras.models

doesn't Python reference tf as an alias to tensorflow from the line import tensorflow as tf?

Benny K
  • 1,957
  • 18
  • 33

2 Answers2

3

When you do

import tensorflow as tf

it has already imported all the sub-modules as tensorflow is a package.

So, to get the models, you just need to access it, no need to import

tf.keras.models

If you wanna import a specific module, then,

from tensorflow.keras import models
Vishnudev Krishnadas
  • 10,679
  • 2
  • 23
  • 55
1

Try using with tensorflow

import tensorflow as tf
import tensorflow.keras.models

Or you can use it as

import tensorflow as tf
sys.modules['tf'] = tensorflow
import tf.keras.models
Sundeep Pidugu
  • 2,377
  • 2
  • 21
  • 43