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,807
  • 14
  • 26

2 Answers2

2

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
  • 9,498
  • 1
  • 15
  • 50
  • Are you sure? according to https://stackoverflow.com/questions/49641621/do-i-need-to-import-submodules-directly , I should import each submodule explicitly. – Benny K Oct 15 '19 at 09:33
  • 1
    You need to import specific submodule explicitly if you only use that submodule. – Vishnudev Oct 15 '19 at 09:51
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,131
  • 2
  • 17
  • 37