Let's face it, the whole business of reloading python code after changing it is a mess. I figured out awhile back that calling import <module>
at the interpreter is better than from <module> import <class/function>
, because then I can call reload(module)
to get updated code.
But I have more complicated issues now. So I have this file, module1.py, and at the top it says:
from module2 import <class1>, <function1>, etc.
And then I go and change code inside module2. Turns out that calling reload(module1)
will not reload the code changed in module2, even though code from module2 is imported at the top of module1. Is there any way to reload everything without restarting the interpreter?
Before anyone gets on my case about style, I'll just say that:
- I only call
reload
from the interpreter, never in active code. This question concerns when I'm testing new code. - I never call from
<module> import *
, I know that destroys readability
Rather than getting better at reloading modules, you could get better at restarting the interpreter. For example, you can put your setup code into its own file, and then run it like this:
This will run setup.py, then leave you at the interactive prompt. Or, rather than doing a lot of work in the interactive prompt, write automated tests that do your work for you.
You are right, reloading modules in Python is a mess. The semantics of the language make it difficult to change code while the process is running. Learn not to need reloading modules, you'll be happier.
Ok, I'm not sure that qualifies as an answer without a change to the code, but... at least, that doesn't involve a change to
module1
.You can use some module wrapper class, that saves loaded modules before and after loading module1 and that provides a
reload
method, something like that:Then load module1 with:
Atfer that you can modify module2 and reload it in interpreter with:
Don't forget that an import is really just assigning a name in a namespace. So, you could reassign that name after reloading:
Now the
class1
object inside module1 refers to the reloaded version from module2.To reload a module, you have to use
reload
, and you have to use it on the module you want to reload. Reloading a module doesn't recursively reload all modules imported by that module. It just reloads that one module.When a module is imported, a reference to it is stored, and later imports of that module re-use the already-imported, already-stored version. When you reload
module1
, it re-runs thefrom module2 import ...
statement, but that just reuses the already-imported version ofmodule2
without reloading it.The only way to fix this is to change your code so it does
import module2
instead of (or in addition to)from module2 import ...
. You cannot reload a module unless the module itself has been imported and bound to a name (i.e., with animport module
statement, not just afrom module import stuff
statement).Note that you can use both forms of the import, and reloading the imported module will affect subsequent
from
imports. That is, you can do this:This can be handy for interactive work, since it lets you use short, unprefixed names to refer to what you need, while still being able to reload the module.
Here is a recursive reload function that you could use (credit to @Matthew): https://stackoverflow.com/a/17194836/1020625
Have a look into IPython. It has the autoreload extension that automatically reloads modules during the interpreter session before calling functions within. I cite the example from the landing page: