Reloading nested modules

Discussion in 'Python' started by Andy Jewell, Jul 13, 2003.

  1. Andy Jewell

    Andy Jewell Guest

    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
    Andy Jewell, Jul 13, 2003
    #1
    1. Advertising

  2. Andy Jewell

    Greg Fortune Guest

    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

    Martin v. Löwis wrote:

    > Andy Jewell <> writes:
    >
    >> Does anyone know of a way to dynamically reload all the imported
    >> modules of a client module?

    >
    > You could iterate over sys.modules and invoke reload for all of them.
    >
    > Regards,
    > Martin
    Greg Fortune, Jul 14, 2003
    #2
    1. Advertising

  3. Greg Fortune <> writes:

    > 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
    Martin v. =?iso-8859-15?q?L=F6wis?=, Jul 14, 2003
    #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. OKB (not okblacke)

    Importing/reloading modules

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

    reloading modules

    Joseph Krauze, Dec 3, 2003, in forum: Python
    Replies:
    0
    Views:
    280
    Joseph Krauze
    Dec 3, 2003
  3. aurora
    Replies:
    2
    Views:
    278
    Bryan
    Oct 16, 2004
  4. Guest
    Replies:
    2
    Views:
    259
    Lee Harr
    Oct 15, 2004
  5. Peter Peyman Puk

    Importing v reloading modules modules

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

Share This Page