Debugging Modules/Packages

Peter Hansen peter at engcorp.com
Thu Mar 27 15:45:05 EST 2003


Brian Olsen wrote:
> 
> Is there a way to make changes to modules and packages while the
> module or package is still in memory? I tried to do something like
> this:
> 
> import sys
> 
> try:
>   del sys.modules["myPackage.whatever"]
> except KeyError:
>   del sys.modules["myPackage"]
> 
> This will allow me to make changes to modules, because Python has to
> constantly re-import the specified packages, but I am wondering if
> there is a better way to doing this.

What you are looking for is the builtin function reload().

Either that, or you want to dynamically rebind the names in
the module to new functions or other objects created on the fly.
Kind of depends if you are trying to update the running system
with code whose *source* you have changed (use reload()) or 
with other changes made programmatically to the modules as the
program runs (avoid reload()).

-Peter




More information about the Python-list mailing list