简体   繁体   中英

“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?

The xy 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM