I want to dynamically reload a module...

Benoit Dupire bdupire at seatech.fau.edu
Fri Mar 23 12:17:20 EST 2001


My goal is to dynamically import  a module called 'func' and to get the
references to the objects that 'func' created.
It works like this:
prog1.py  dynamically imports func.py.
func.py creates a setheading object, and prog1 gets its reference. All
works fine !
The result is:
>>>reload(prog1)
test
<module 'prog1' from 'C:\Python20\prog1.pyc'>


# prog1.py

class foo:
    def __init__(self, module):
        self.aa=__import__(module)          # dynamically import func
        chRef= getattr(self.aa, 'setheading')         # get reference to
the object that func created.
        chRef.run()

my_foo = foo('func')

# ----------------------------------
# func.py
class SetHeading:
    def run(self):
        print 'test'

setheading=SetHeading()


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! this shouldn't work...

How can I force prog1.py to dynamically reload  module='func' ?
I did not see any __reload__ function in the Library Reference ....






More information about the Python-list mailing list