Reload in Python interpreter

xralf picture xralf · May 1, 2012 · Viewed 12.1k times · Source
$ python
>>> import myapp
>>> reload(myapp)
<module 'myapp' from 'myapp.pyc'>
>>>

ctrl+D

$ python
>>> from myapp import *
>>> reload(myapp)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'myapp' is not defined

Why this behaves differently? How can I reload when using from myapp import *?

Answer

aland picture aland · May 1, 2012

From http://docs.python.org/library/functions.html#reload :

If a module imports objects from another module using from ... import ..., calling reload() for the other module does not redefine the objects imported from it — one way around this is to re-execute the from statement, another is to use import and qualified names (module.name) instead.

So, you should do something like:

from myapp import *
....
import myapp
reload(myapp)
from myapp import *