Skip to main content
8-Gravel
January 18, 2023
Question

In Python, how does the "as" clause influence "import"?

  • January 18, 2023
  • 2 replies
  • 905 views

I assumed that the "as" phrase just created an alias for an imported module. However, in the following code, "import" fails with the "as" phrase.

 

 

import tensorflow.python.eager as eager

 

 

The following error occurs as a result of this statement.

 

 

Traceback (most recent call last):
 File "/home/snippet/prof/importer.py", line 2, in <module>
 import tensorflow.python.eager as eager
AttributeError: module 'tensorflow' has no attribute 'python'

 

 

When I delete the "as" clause, the "import" function works well. What effect does the "as" clause have on the success or failure of "import"?

This article suggests not using ".eager" when importing "eager" as anything and suggests the following:

 

 

from tensorflow.python import eager as eager

 

 

This works great, however, I'm not sure why my example is incorrect. could someone please explain

 

2 replies

Community Manager
January 24, 2023

Hi @sachinbhatt.

 

This post doesn't appear to be ThingWorx related.   Can you confirm?

 

Regards.

 

--Sharon

24-Ruby III
January 25, 2023

You can ask about it here: https://python-forum.io/