[Python-talk] Modules force recompilation?

Kent Johnson kent37 at tds.net
Sat Feb 6 15:07:04 EST 2010


On Sat, Feb 6, 2010 at 12:34 PM, Bruce Labitt
<bruce.labitt at myfairpoint.net> wrote:

> It would appear this is a common 'issue' with python.
> Reload doesn't seem to alway do what one expects...
>
> What then is a good way to force the recompilation and module cache flush?
>
> /code snippet of my main
>
> if __name__ == '__main__':
>   import uladata as ud
>   import targets as tgt
>   ps = persistentstorage()
>
>   ...
>   TGT = tgt.getTGT( p, sqrt(sig2) )
>   ...
>   YY = ud.myuladata(TGT, m, k, d, t, epsilon, arrayerror, chatty)
>   ...
>
>
> If I edit anything in uladata.py, or targets.py, (and save it of course) how
> can I force the recompile of either or both modules, while in the ipython
> environment?

In ipython, if you
import uladata
import targets
reload(uladata)
reload(targets)

it should do what you want. You only have to do the imports once; the
reload has to be after each change.

> As I indicated in my original post, maybe somewhat unclearly, if I
>
> %run main.py
>
> after editing either uladata or targets, I execute old code and not the new
> code.

You could put the reloads into main.py for development.

> Anyone have some insight on how to do this within the ipython environment?
>  Or some other environment?

You might look at
http://www.indelible.org/ink/python-reloading/
http://code.google.com/p/livecoding/

In most environments this is not an issue because each execution uses
a new instance of the python interpreter. For example running from the
command line directly. Many editors have good support for running
python programs also, so you might run main from the same editor you
are using to change it. Again, the editor will start a new interpreter
for each execution so it is not an issue.

Kent


More information about the Python-talk mailing list