Sub topic 11
Reloading a Module After You Edit It
This chapter assumes you can start the shell, import modules, and read a traceback. It covers one problem: you changed a module's file, and the open shell still shows the old behavior.
Why a second import does nothing
When you run import mymodule, Python finds the file, executes it once, and stores the resulting module object in sys.modules. A later import mymodule finds that entry and returns it without reading the file again. Editing the file on disk does not change the module object in memory.
Here is a small module to work with. Save it as mymodule.py in the directory where you start the shell:
def greet(name):
return "Hello, " + nameStart the shell in that directory and try it:
>>> import mymodule
>>> mymodule.greet("Ana")
'Hello, Ana'Now change the function in your editor so it returns "Hi, " + name, and save the file. Back in the shell:
>>> import mymodule
>>> mymodule.greet("Ana")
'Hello, Ana'The output is unchanged because the second import reused the cached module.
Using importlib.reload()
The importlib module provides reload(). It runs the module's file again and updates the existing module object in place.
>>> import importlib
>>> importlib.reload(mymodule)
<module 'mymodule' from '/home/ana/mymodule.py'>
>>> mymodule.greet("Ana")
'Hi, Ana'The shell echoes the module because reload() returns it. The path in your output will differ. Note that reload() takes the module object, not a string, so the module must already be imported under that name.
What reload does not update
Reloading updates the module object, but it cannot reach names you copied out of it earlier. This is the most common source of confusion.
>>> from mymodule import greet
>>> importlib.reload(mymodule)
<module 'mymodule' from '/home/ana/mymodule.py'>
>>> greet("Ana")After you edit the file again and reload, greet still points to the function object from the earlier run. The name mymodule.greet points to the new one. The same applies to objects you created from the old code. An instance of a class defined in the old version keeps using the old class.
To refresh a name, import it again after the reload:
>>> importlib.reload(mymodule)
<module 'mymodule' from '/home/ana/mymodule.py'>
>>> from mymodule import greetOther limits
- Reload re-executes the whole file. Top-level code runs again, including any
print()calls or side effects such as opening files. - Names you delete from the file stay on the module object, because reload runs the new code in the old module's namespace. Remove stale names yourself with
del mymodule.old_name, or restart the shell. - If the edited file has a syntax error,
reload()raisesSyntaxError. The old module object stays in place and usable. - Other modules that imported from your module hold their own references, and they are not updated either.
Choosing between reload and restart
| Situation | Better choice |
|---|---|
| Small edit to a function body, accessed as `mymodule.func` | `importlib.reload(mymodule)` |
| You used `from mymodule import name` | Reload, then import the name again |
| You changed class definitions and have live instances | Restart and rebuild the instances |
| Module has expensive or side-effecting top-level code | Restart, or rerun with `python3 -i` |
The last row connects to the next chapter. Running python3 -i script.py starts a fresh process each time, so you never depend on reload behaving correctly.
Exercise
You run from mymodule import greet, edit greet in the file, and call importlib.reload(mymodule). Calling greet("Ana") still gives the old result, but mymodule.greet("Ana") gives the new one. Why, and how do you fix it?
Show answer
from mymodule import greet copied a reference to the old function object into your session's namespace. Reload creates a new function and rebinds mymodule.greet, but the separate name greet still points to the old one. Run from mymodule import greet again after the reload, or call mymodule.greet instead.
Exercise
What happens if you call importlib.reload(mymodule) after saving a file that contains a syntax error?
Show answer
Python raises SyntaxError. The reload fails, and the previously loaded mymodule object stays in memory and keeps working, so you can fix the file and reload again.