The import Statement

In the first example, the name module1 serves two different purposes—it identifies an external file to be loaded, and it becomes a variable in the script, which references the module object after the file is loaded:

>>> import module1                         # Get module as a whole
>>> module1.printer('Hello world!')        # Qualify to get names
Hello world!

广告:个人专属 VPN,独立 IP,无限流量,多机房切换,还可以屏蔽广告和恶意软件,每月最低仅 5 美元

Because import gives a name that refers to the whole module object, we must go through the module name to fetch its attributes (e.g., module1.printer).