471,078 Members | 835 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,078 software developers and data experts.

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?
Mar 2 '06 #1
3 2026
On Thu, 2 Mar 2006, John Salerno wrote:
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?


"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
Mar 2 '06 #2
Dave Benjamin wrote:
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.


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!) :)
Mar 2 '06 #3
On Thu, 2 Mar 2006, John Salerno wrote:
Dave Benjamin wrote:
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.


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!) :)


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
Mar 2 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Mustafa Thamer | last post: by
2 posts views Thread by Wolfgang.Stoecher | last post: by
2 posts views Thread by beliavsky | last post: by
49 posts views Thread by Martin Unsal | last post: by
6 posts views Thread by HMS Surprise | last post: by
reply views Thread by Gary Herron | last post: by
reply views Thread by Gabriel Genellina | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.