Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"from x import y as z" vs. "import x.y as z"

I'm assuming they are functionally the same, bar some negligible under-the-hood differences. If so, which form is more Pythonic?

like image 620
tshepang Avatar asked Feb 17 '11 11:02

tshepang


1 Answers

The x.y form makes it implicit that packages and modules are involved, and should be the preferred form when that is the case.

If t is a symbol defined in module y, then:

>>> from x.y import t as z
>>>

...but!

>>> import x.y.t as z
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named t
>>> 

The dot notation is reserved for modules, and should be used when modules are involved.

like image 137
Apalala Avatar answered Nov 07 '22 03:11

Apalala