views:

123

answers:

5

scenario: a modular app that loads .py modules on the fly as it works. programmer (me) wishes to edit the code of a module and and then re-load it into the program without halting execution.

can this be done?

i have tried running import a second time on an updated module.py, but the changes are not picked up

A: 

reload() will do what you need. You just have to be careful about what code executes in your module upon re-compile; it is usually easiest if your module just contains definitions.

msw
A: 

use the builtin command:

reload(module)

http://stackoverflow.com/questions/437589/how-do-i-unload-reload-a-python-module

Jeremiah Rose
+6  A: 

While reload does reload a module, as the other answer mentions, you need quite a few precautions to make it work smoothly -- and for some things you might believe would work easily, you're in for quite a shock in terms of amount of work actually needed.

If you ever use the form from module import afunction, then you've almost ensured reload won't work: you must exclusively import modules, never functions, classes, etc, from inside modules, if you want to have any hope of reload doing something useful all all (otherwise you'd have to somehow chases all the bits and pieces imported here and there from the module, and rebind each and every one of them -- eep;-). Note that I prefer following this rule anyway, whether I plan to do any reloading or not, but, with reload, it's crucial.

The difficult problem is: if you have, alive anywhere, instances of classes that existed in the previous version of the module, reload per see will do absolutely nothing to upgrade those instances. That problem is a truly hard one; one of the longest, hardest recipes in the Python Cookbook (2nd edition) is all about how to code your modules to support such "reload that actually upgrades existing instances". This only matter if you program in OOP style, of course, but... any Python program complex enough to need "reload this plugin" functionality is very likely to have lots of OOP in it, so it's hardly a minor issue.

The docs for reload are pretty complete and do mention this issue, but give no hint how to solve it. This recipe by Michael Hudson, from the Python Cookbook online, is better, but it's only the start of what we evolved into the printed (2nd edition) -- recipe 20.15, the online version of that is here (incomplete unless you sign up for a free time-limited preview of O'Reilly' commercial online books service).

Alex Martelli
It might be possible to implement a restricted "reload" just for the modules the OP is interested in using some homebrew APIs.
Noufal Ibrahim
A: 

You may get some use from the livecoding module.

Kylotan
A: 

Alex's answer and the others cover the general case.

However, since these modules you're working on are your own and you'll edit only them, it's possible to implement some kind of a local reloading mechanism rather than rely on the builtin reload. You can architect your module loader as something that loads up the files, compiles it and evals them into a specific namespace and tells the calling code that it's ready. You can take care of reloading by making sure that there's a decent init function which reinitialises the module properly. I assume you'll be editing only these.

It's a bit of work but might be interesting and worth your time.

Noufal Ibrahim