reloading my own modules

Discussion in 'Python' started by Lowell Kirsh, Jun 7, 2005.

  1. Lowell Kirsh

    Lowell Kirsh Guest

    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
    Lowell Kirsh, Jun 7, 2005
    #1
    1. Advertising

  2. 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
    Skip Montanaro, Jun 7, 2005
    #2
    1. Advertising

  3. Lowell Kirsh

    Lowell Kirsh Guest

    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.
    Lowell Kirsh, Jun 8, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy Jewell

    Reloading nested modules

    Andy Jewell, Jul 13, 2003, in forum: Python
    Replies:
    2
    Views:
    300
    Martin v. =?iso-8859-15?q?L=F6wis?=
    Jul 14, 2003
  2. OKB (not okblacke)

    Importing/reloading modules

    OKB (not okblacke), Sep 4, 2003, in forum: Python
    Replies:
    0
    Views:
    291
    OKB (not okblacke)
    Sep 4, 2003
  3. Joseph Krauze

    reloading modules

    Joseph Krauze, Dec 3, 2003, in forum: Python
    Replies:
    0
    Views:
    286
    Joseph Krauze
    Dec 3, 2003
  4. aurora
    Replies:
    2
    Views:
    284
    Bryan
    Oct 16, 2004
  5. Peter Peyman Puk

    Importing v reloading modules modules

    Peter Peyman Puk, Mar 19, 2010, in forum: Python
    Replies:
    0
    Views:
    296
    Peter Peyman Puk
    Mar 19, 2010
Loading...

Share This Page