沒有區別:
$ python3.2
Python 3.2.5 (default, Mar 10 2014, 10:39:23)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import urllib
>>> import urllib.request as urllib_request
>>> urllib.request is urllib_request
True
兩個import urllib
和import urllib.request
將進口模塊。
然而,表格:from <module> import <object>
會導入所述模塊並將對象返回到您當前的命名空間或模塊中。
實施例:
$ python3.2
Python 3.2.5 (default, Mar 10 2014, 10:39:23)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from urllib.request import urlopen
>>> urlopen
<function urlopen at 0x1015f6af0>
注意urlopen
是一個函數。還要注意:
>>> import sys
>>> sys.modules["urllib"]
<module 'urllib' from '/usr/local/Cellar/python32/3.2.5/Frameworks/Python.framework/Versions/3.2/lib/python3.2/urllib/__init__.py'>
>>> sys.modules["urllib.request"]
<module 'urllib.request' from '/usr/local/Cellar/python32/3.2.5/Frameworks/Python.framework/Versions/3.2/lib/python3.2/urllib/request.py'>
通過導入urllib.request.urlopen
還導入模塊:urllib
和urllib.request
。
參見:https://docs.python.org/3.4/tutorial/modules.html
十分感謝,非常明確的解釋。 –