Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reloading module which has been imported to another module

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:

  1. I only call reload from the interpreter, never in active code. This question concerns when I'm testing new code.
  2. I never call from <module> import *, I know that destroys readability
like image 747
J-bob Avatar asked Dec 23 '12 23:12

J-bob


2 Answers

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 the from module2 import ... statement, but that just reuses the already-imported version of module2 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 an import module statement, not just a from 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:

>>> import module
>>> from module import x
>>> x
2
# Change module code here, changing x to 3
>>> reload(module)
>>> from module import x
>>> x
3

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.

like image 37
BrenBarn Avatar answered Nov 08 '22 04:11

BrenBarn


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:

In [1]: %load_ext autoreload

In [2]: %autoreload 2

In [3]: from foo import some_function

In [4]: some_function()
Out[4]: 42

In [5]: # open foo.py in an editor and change some_function to return 43

In [6]: some_function()
Out[6]: 43
like image 73
ojdo Avatar answered Nov 08 '22 04:11

ojdo