预计阅读本页时间:-
Module Imports and Reloads
So far, I’ve been talking about “importing modules” without really explaining what this term means. We’ll study modules and larger program architecture in depth in Part V, but because imports are also a way to launch programs, this section will introduce enough module basics to get you started.
In simple terms, every file of Python source code whose name ends in a .py extension is a module. Other files can access the items a module defines by importing that module; import operations essentially load another file and grant access to that file’s contents. The contents of a module are made available to the outside world through its attributes (a term I’ll define in the next section).
广告:个人专属 VPN,独立 IP,无限流量,多机房切换,还可以屏蔽广告和恶意软件,每月最低仅 5 美元
This module-based services model turns out to be the core idea behind program architecture in Python. Larger programs usually take the form of multiple module files, which import tools from other module files. One of the modules is designated as the main or top-level file, and this is the one launched to start the entire program.
We’ll delve into such architectural issues in more detail later in this book. This chapter is mostly interested in the fact that import operations run the code in a file that is being loaded as a final step. Because of this, importing a file is yet another way to launch it.
For instance, if you start an interactive session (from a system command line, from the Start menu, from IDLE, or otherwise), you can run the script1.py file you created earlier with a simple import (be sure to delete the input line you added in the prior section first, or you’ll need to press Enter for no reason):
C:\misc> c:\python30\python
>>> import script1
win32
1267650600228229401496703205376
Spam!Spam!Spam!Spam!Spam!Spam!Spam!Spam!
This works, but only once per session (really, process) by default. After the first import, later imports do nothing, even if you change and save the module’s source file again in another window:
>>> import script1
>>> import script1
This is by design; imports are too expensive an operation to repeat more than once per file, per program run. As you’ll learn in Chapter 21, imports must find files, compile them to byte code, and run the code.
If you really want to force Python to run the file again in the same session without stopping and restarting the session, you need to instead call the reload function available in the imp standard library module (this function is also a simple built-in in Python 2.6, but not in 3.0):
>>> from imp import reload # Must load from module in 3.0
>>> reload(script1)
win32
65536
Spam!Spam!Spam!Spam!Spam!Spam!Spam!Spam!
<module 'script1' from 'script1.py'>
>>>
The from statement here simply copies a name out of a module (more on this soon). The reload function itself loads and runs the current version of your file’s code, picking up changes if you’ve changed and saved it in another window.
This allows you to edit and pick up new code on the fly within the current Python interactive session. In this session, for example, the second print statement in script1.py was changed in another window to print 2 ** 16 between the time of the first import and the reload call.
The reload function expects the name of an already loaded module object, so you have to have successfully imported a module once before you reload it. Notice that reload also expects parentheses around the module object name, whereas import does not. reload is a function that is called, and import is a statement.
That’s why you must pass the module name to reload as an argument in parentheses, and that’s why you get back an extra output line when reloading. The last output line is just the display representation of the reload call’s return value, a Python module object. We’ll learn more about using functions in general in Chapter 16.
Note
Version skew note: Python 3.0 moved the reload built-in function to the imp standard library module. It still reloads files as before, but you must import it in order to use it. In 3.0, run an import imp and use imp.reload(M), or run a from imp import reload and use reload(M), as shown here. We’ll discuss import and from statements in the next section, and more formally later in this book.
If you are working in Python 2.6 (or 2.X in general), reload is available as a built-in function, so no import is required. In Python 2.6, reload is available in both forms—built-in and module function—to aid the transition to 3.0. In other words, reloading is still available in 3.0, but an extra line of code is required to fetch the reload call.
The move in 3.0 was likely motivated in part by some well-known issues involving reload and from statements that we’ll encounter in the next section. In short, names loaded with a from are not directly updated by a reload, but names accessed with an import statement are. If your names don’t seem to change after a reload, try using import and module.attribute name references instead.