14.2 Reloading All Loaded Modules
Credit: Sébastien Keim
14.2.1 Problem
When you repeatedly run a test
script during an interactive session, it always uses the first
version of the modules you are developing, even if you made changes
to the code. You need to ensure that modules are reloaded.
14.2.2 Solution
There are a few ways to accomplish this goal. Here's
a solution that is simple and drastic, but it may not work with
integrated
development environments (IDEs):
import sys
sys.modules.clear( )
And here is a solution that is a bit more careful and is compatible
with IDEs:
import sys
if globals( ).has_key('init_modules'):
# second or subsequent run: remove all but initially loaded modules
for m in sys.modules.keys( ):
if x not in init_modules:
del(sys.modules[m])
else:
# first run: find out which modules were initially loaded
init_modules = sys.modules.keys( )
14.2.3 Discussion
When you create a Python module, you can use a test script that
imports your module. But you have probably noticed that when you
repeatedly run the test script inside a given interactive session, it
always uses the first version of your module, even if you made
changes in the code. This is because the
import statement checks if the module is already
in memory and does the actual importing only if this is the first
time the module is used. This is an important optimization that lets
you use the import statement freely, but it does
get in the way in such development situations.
You can use the
reload
function, but this is difficult if you perform changes in a module
that isn't directly imported by your test script.
One simple solution is to remove all
modules from memory before running the test script. For this, two
lines at the start of your test script, as shown in the first
solution, suffice.
If you work with a framework that executes user code in the same
interpreter as the IDE itself (such as IDLE), however, you will
notice that this technique fails. This is because
sys.modules.clear removes IDLE from memory, so you
will have to use the second solution in that case. On the first run,
the solution determines which modules are initial modules for the
system (all those that are loaded at this point). On all other runs,
the solution cleans up all modules whose names are not in this
initial list. This, of course, relies on
globals
(i.e., the dictionary of this test script, seen as a module) being
unchanged when this test script is run again.
14.2.4 See Also
Documentation on the sys standard library module,
along with the reload and
globals built-ins, in the Library
Reference; the section on the import
statement in the Language Reference.
|