import, from and reload

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Salerno

    #1

    import, from and reload

    I understand that after you import something once, you can reload it to
    pick up new changes. But does reload work with from statements? I tried
    this:

    from X import *

    and then did my testing. I changed X and tried to reload it, but that
    didn't seem to work. I figure the reason is because the module itself
    doesn't exist as an object, only its names do. But I couldn't figure out
    how to pick up my new changes at this point. I think eventually I did

    import X

    and that sort of started me back from the beginning, with my changes.
    But is there a way to continue to use a from statement instead, and then
    reload your changes?
  • Dave Benjamin

    #2
    Re: import, from and reload

    On Thu, 2 Mar 2006, John Salerno wrote:
    [color=blue]
    > I understand that after you import something once, you can reload it to pick
    > up new changes. But does reload work with from statements? I tried this:
    >
    > from X import *
    >
    > and then did my testing. I changed X and tried to reload it, but that didn't
    > seem to work. I figure the reason is because the module itself doesn't exist
    > as an object, only its names do. But I couldn't figure out how to pick up my
    > new changes at this point. I think eventually I did
    >
    > import X
    >
    > and that sort of started me back from the beginning, with my changes. But is
    > there a way to continue to use a from statement instead, and then reload your
    > changes?[/color]

    "reload" is an ordinary procedure that takes a module as an argument. When
    you use a "from X import *" statement, "X" is not imported, so you have no
    module object to pass to "reload". In addition, even if you do import "X"
    and reload it, you won't update your bindings; you'll still have to do
    "from X import *" again to update any names imported from X before.

    So, to make a long story short, you have to do something like:

    import X
    reload(X)
    del X # to keep your namespace clean
    from X import *

    In general, "from X import *" should be avoided anyway, for reasons that
    have been discussed many times in the past. The annoyance with reloading
    is just one more reason. Better to just use "import X" in the first place.

    --
    .:[ dave benjamin -( ramen/sp00 )- http://spoomusic.com/ ]:.
    "one man's constant is another man's variable" - alan perlis

    Comment

    • John Salerno

      #3
      Re: import, from and reload

      Dave Benjamin wrote:
      [color=blue]
      > In general, "from X import *" should be avoided anyway, for reasons that
      > have been discussed many times in the past. The annoyance with reloading
      > is just one more reason. Better to just use "import X" in the first place.
      >[/color]

      Thanks. I kind of figured it's better to use import instead of from
      anyway, but I was following along with some examples that use from
      (despite the fact that earlier in the book they even say that from is
      problematic and you should use import instead!) :)

      Comment

      • Dave Benjamin

        #4
        Re: import, from and reload

        On Thu, 2 Mar 2006, John Salerno wrote:
        [color=blue]
        > Dave Benjamin wrote:
        >[color=green]
        >> In general, "from X import *" should be avoided anyway, for reasons that
        >> have been discussed many times in the past. The annoyance with reloading is
        >> just one more reason. Better to just use "import X" in the first place.[/color]
        >
        > Thanks. I kind of figured it's better to use import instead of from anyway,
        > but I was following along with some examples that use from (despite the fact
        > that earlier in the book they even say that from is problematic and you
        > should use import instead!) :)[/color]

        No problem. I stand by my original advice, but there is one semi-oneliner
        that you might find useful:

        reload(__import __('X')); from X import *

        You could keep that in your clipboard and paste it into the interpreter
        when you need to reload.

        --
        .:[ dave benjamin -( ramen/sp00 )- http://spoomusic.com/ ]:.
        "one man's constant is another man's variable" - alan perlis

        Comment

        Working...