Skip to main content
8-Gravel
Jan 18, 2023 7:13 AM UTC
Question

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

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

Support
Jan 24, 2023 4:25 PM UTC

Hi @sachinbhatt.

 

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

 

Regards.

 

--Sharon

Translate
24-Ruby IV
Jan 25, 2023 6:41 AM UTC

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

Translate