37,743
社区成员




$ python3
Python 3.5.2 (default, Nov 17 2016, 17:05:23)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> 'path' in dir(os)
True
>>> import os.path
>>> 'join' in dir(os.path)
True
>>> import urllib
>>> 'request' in dir(urllib)
False
>>> import urllib.request
>>> 'request' in dir(urllib)
True
>>> 'urlopen' in dir(urllib.request)
True
如果只是import urllib,那么其名字空间中是没有request的,所以urllib.request.urlopen会出错。
如果推测正确,os应该是个常规包,而urllib应该是个名字空间包。
Python3我不是很熟,可以自行检阅:
https://docs.python.org/3.6/reference/import.html?highlight=namespace#the-import-system
https://docs.python.org/3.6/reference/import.html?highlight=namespace#regular-packages
https://docs.python.org/3.6/reference/import.html?highlight=namespace#namespace-packages