1

If as is used to create an alias in python such as

import tensorflow as tf

why cannot I import using the same alias

import tensorflow as tf
from tf import keras

Gives me error

ModuleNotFoundError: No module named 'tf'
Imanpal Singh
  • 1,105
  • 1
  • 12
  • 22

3 Answers3

2

tf is not intended to be a keyword or Module name. Searching about tf will not be found in the Module list. See this

I_Al-thamary
  • 3,385
  • 2
  • 24
  • 37
0

The error is beacuse the,

from tf import keras 

directly tries to find a module named tf it does not tries to look for variable named tf or any other declaration of tf in your code.

We can import a module with a specific name but the loading process is based on module name and not the alias you have created for it.

Hope this helps!

Prathamesh
  • 1,064
  • 1
  • 6
  • 16
0

I think that while importing you have to use the original name as [Because tf is not a module,it is just a nickname for that library.]

from tensorflow import keras

and at the time of using it you can use

tf.keras()

(I am not 100% sure,you can try it.)

Rohan Nagmoti
  • 31
  • 1
  • 7