Reloading modules

Thomas Wouters thomas at xs4all.nl
Sun Jun 25 13:08:21 EDT 2000


On Sun, 25 Jun 2000 17:09:36 +0100, Duncan Smith
<buzzard at urubu.freeserve.co.uk> wrote:
>>>> from Graph import *

>>>> reload(Graph)
>Traceback (innermost last):
>  File "<interactive input>", line 1, in ?
>TypeError: reload() argument must be module

>What do I need to do to successfully reload a module?  I'm currently wasting
>a lot of time re-starting PythonWin each time I want to test a changed
>module and I can't find the answer.  Thanks

You do want reload(), but you have to give it a module object (not the name,
but the imported object.) 'from Graph import *' doesn't bind the module
object to a local name, but apparently something else in your module is
named Graph, and that object isn't a module.

If you *have* to use both reload() and from Graph import *, do something
like this:

import sys
from Graph import *
...
...
reload(sys.modules['Graph'])
from Graph import *
...
...

The 'import *' after the reload is necessary to re-bind all local names from
the Graph module to the new values in the reloaded Graph module. If you
leave it out, the reload() is pretty useless. The sys.modules trick avoids
having to import Graph seperately, and getting it overwritten by
Graph.Graph.

But personally, I'd myself a lot of pain and not use 'import *'. It bites in
more way than one ;-)

Just-say-no-ly y'rs,
Thomas.



More information about the Python-list mailing list