`from … import` vs `import .` [duplicate]

2019-01-01 14:02发布

This question already has an answer here:

I'm wondering if there's any difference between the code fragment

from urllib import request

and the fragment

import urllib.request

or if they are interchangeable. If they are interchangeable, which is the "standard"/"preferred" syntax (if there is one)?

Thanks!

7条回答
梦寄多情
2楼-- · 2019-01-01 14:58

You are using Python3 were urllib in the package. Both forms are acceptable and no one form of import is preferred over the other. Sometimes when there are multiple package directories involved you may to use the former from x.y.z.a import s

In this particular case with urllib package, the second way import urllib.request and use of urllib.request is how standard library uniformly uses it.

查看更多
登录 后发表回答