Reloading nested modules

A

Andy Jewell

Does anyone know of a way to dynamically reload all the imported modules of a
client module?

I'm writing a program that I have broken down into quite a few submodules, and
the 'configuration' is done with modules too. As I am developing the app, I
need to test bits and pieces here and there. This is what I currently do:

In each module, I have a section, just after the main imports like so:


-----------8<-----------
# normal imports:

from ooby import squibble,dibble
import dooby
import doo

# reloads for debugging

import ooby # create a reference to ooby so we can reload it
reload(ooby)
reload(dooby)
reload(doo)

# main module code begins...
-----------8<-----------


The debugging imports of course will be dropped when the program is stable.

I also often find myself doing similar stuff from the command-line, like:

reload(ooby.oojar); reload(ooby.dibble); reload(ooby); ooby.somefunc(.....)

I just wondered if anyone had develped a better 'spell' or even a small script
that uses some clever intorspecton hack, before I go and start trying to
reinvent the wheel...

-andyj
 
G

Greg Fortune

I've wondered about the same problem and considered that as a solution, but
never tested it. I assume that *wouldn't* rebind all of the imports that
have already happened.. ie, I think the references to the old copies of
the modules would hang around even though new ones have been imported.

Regardless, the scoping doesn't work so an attempt to reload the embedded
module assumes it is available in local scope and fails. Again, that would
lead me to believe my first statement is true, but when I tested just now,
I got no further than the scoping problem...

I've been intended to write something that will take a module name and
rebind it in all namespaces that have it currently, but haven't got around
to it. If I ever do, I'll post it here :)

Greg
 
M

Martin v. =?iso-8859-15?q?L=F6wis?=

Greg Fortune said:
I've wondered about the same problem and considered that as a solution, but
never tested it. I assume that *wouldn't* rebind all of the imports that
have already happened.. ie, I think the references to the old copies of
the modules would hang around even though new ones have been imported.

Depends on the import. For

import foo

the reload would take effect, as, on reload, the module object stays,
its dictionary stays, and it is just the dictionary contents that is
recreated.

For

from foo import bar

you still have the old value of bar after reloading.
Regardless, the scoping doesn't work so an attempt to reload the embedded
module assumes it is available in local scope and fails. Again, that would
lead me to believe my first statement is true, but when I tested just now,
I got no further than the scoping problem...

What scoping problem?
I've been intended to write something that will take a module name and
rebind it in all namespaces that have it currently, but haven't got around
to it. If I ever do, I'll post it here :)

Just ask Guido to borrow you the time machine - this has already been
done.

Regards,
Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top