reloading my own modules

L

Lowell Kirsh

I have a driver module as well as several other modules. I am running
the driver interactively from emacs - that is, I don't restart python on
each run. I want to work it such that every time a modify the source for
one of the non-driver modules and re-run the driver, the other modules
will be reloaded. I have the following at the top of my driver module:

import dbtest, util
for module in ['dbtest', 'util']:
if module in sys.modules.keys():
reload(sys.modules[module])

Is this the best way to do it? It seems a bit verbose, but not really
all that bad. I was just wondering if there is a more standard way to do it?

Lowell
 
S

Skip Montanaro

Lowell> import dbtest, util
Lowell> for module in ['dbtest', 'util']:
Lowell> if module in sys.modules.keys():
Lowell> reload(sys.modules[module])

Lowell> Is this the best way to do it? It seems a bit verbose, but not
Lowell> really all that bad. I was just wondering if there is a more
Lowell> standard way to do it?

Not that I'm aware of. You don't need to request the keys() of a dict in
recent versions of Python:

import dbtest, util
for module in ['dbtest', 'util']:
if module in sys.modules:
reload(sys.modules[module])

Also, since it's clear you have already imported dbtest and util, there's no
need to check in sys.modules:

import dbtest, util
reload(dbtest)
reload(util)

Skip
 
L

Lowell Kirsh

Hi,

Skip said:
Also, since it's clear you have already imported dbtest and util, there's no
need to check in sys.modules:

import dbtest, util
reload(dbtest)
reload(util)

Won't this load the modules twice on the first run? I only want to load
the modules once each time the script is run.
 

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

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top