Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > reloading my own modules

Reply
Thread Tools

reloading my own modules

 
 
Lowell Kirsh
Guest
Posts: n/a
 
      06-07-2005
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
 
Reply With Quote
 
 
 
 
Skip Montanaro
Guest
Posts: n/a
 
      06-07-2005

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
 
Reply With Quote
 
 
 
 
Lowell Kirsh
Guest
Posts: n/a
 
      06-08-2005
Hi,

Skip Montanaro wrote:
> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Importing v reloading modules modules Peter Peyman Puk Python 0 03-19-2010 05:09 PM
reloading modules Joseph Krauze Python 0 12-03-2003 05:18 PM
Importing/reloading modules OKB (not okblacke) Python 0 09-04-2003 05:25 AM
Reloading nested modules Andy Jewell Python 2 07-14-2003 08:21 PM



Advertisments