dynamically reload a module...Solution

Benoit Dupire bdupire at seatech.fau.edu
Sat Mar 24 18:40:59 EST 2001


I  post here the answer to my question, as it may interest other people.
It was found by Daniel Yoo and Michael P Reilly. Thanks a lot.
Here is a summary.....

Benoit Dupire wrote:

> My goal is to dynamically import  a module called 'func' from prog1.py and
> to get the
> references to the objects created by func
>
> # prog1.py
>
> class foo:
>     def __init__(self, module):
>         self.aa=__import__(module)          # dynamically import func
>         chRef= getattr(self.aa, 'setheading')
>         chRef.run()
>
> my_foo = foo('func')
>
> # ----------------------------------
> # func.py
> class SetHeading:
>     def run(self):
>         print 'test'
>
> setheading=SetHeading()
>

> It works fine..
> Now I change func.py, replacing ' setheading=SetHeading()' with
> 'toto=SetHeading()'

>
> Normally, this should create an error when I run prog1.py, because the
> setheading object does not exist anymore...
> However:
> >>> reload(prog1)
> test
> <module 'prog1' from 'C:\Python20\prog1.pyc'>
>
> func is not reimported!
>
> How can I force prog1.py to dynamically reload  module='func' ?
> I did not see any __reload__ function in the Library Reference ....

I already tried reload() but it didn't seem to work.
prog1.py becomes:

# prog1.py
class foo:
    def __init__(self, module):
        self.aa=__import__(module)
        reload(self.aa)
        chRef= getattr(self.aa, 'stHeading')
        chRef.run()

my_foo = foo('func')


And the results....

>>> import prog1
test

That works fine...!

Now change func.py, replacing
'setheading=SetHeading()'
with 'toto=SetHeading()'

>>> reload(prog1)
test
<module 'prog1' from 'C:\Python20\prog1.pyc'>

Why does this still work ? setheading does not exist anymore....
I had no clue... until i run this from the command line, where i got the
correct
results, and not from IDLE.

Could someone explain me why IDLE is messing everything up ?


Daniel Yoo replied:

You're not going to like this:  According to the reference documentation:

"When a module is reloaded, its dictionary (containing the module's global
variables) is retained.  Redefinition of names will override the old
definitions, so this is generally not a problem.  If the new version of a
module does not define a name that was defined by the old version, the old
definition remains."

This comes from:

    http://python.org/doc/current/lib/built-in-funcs.html

So that's why: setheading persists in the module definition if it hasn't
been overridden, so erasing it won't take affect until we restart Python.
This explains the weirdness that was happening before.  Try writing your
code to change the behavior of setheading, instead of removing it
entirely.

I wonder if there's a way of "clearing" out a module's definition, by
calling "del" on its dictionary of methods.  At the moment, I don't have
easy access to a Python interpreter (the horror!  the horror!), so I can't
confirm this yet.

Hope this helps!





More information about the Python-list mailing list