Reloading nested modules

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andy Jewell

    Reloading nested modules

    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.ooj ar); reload(ooby.dib ble); 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

  • Greg Fortune

    #2
    Re: Reloading nested modules

    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:
    [color=blue]
    > Andy Jewell <[email protected]> writes:
    >[color=green]
    >> Does anyone know of a way to dynamically reload all the imported
    >> modules of a client module?[/color]
    >
    > You could iterate over sys.modules and invoke reload for all of them.
    >
    > Regards,
    > Martin[/color]

    Comment

    • Martin v. Löwis

      #3
      Re: Reloading nested modules

      Greg Fortune <lists@gregfort une.com> writes:
      [color=blue]
      > 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.[/color]

      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.
      [color=blue]
      > 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...[/color]

      What scoping problem?
      [color=blue]
      > 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 :)[/color]

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

      Regards,
      Martin

      Comment

      Working...