473,386 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 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 2163
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Phipps Xue | last post by:
Dear all, I wanna reload a script module after it's modified so that I can use the new function immediately. Is there any help for that? It's appreciated very much if any example provided! ...
0
by: Mustafa Thamer | last post by:
Hi, I'm using import hooks according to PEP 302, in order to load python files from a game PAK file. The game is C++ using embedded and extended Python (v2.33) and Boost. The importing works...
2
by: Wolfgang.Stoecher | last post by:
Hi, another beginner's question: when I import a module, change the corresponding textfile and try to import the same module again, it looks like the internal code does not change (the...
2
by: beliavsky | last post by:
Near the beginning of file test_matrix.py from scipy are the lines import scipy.base reload(scipy.base) from scipy.base import * del sys.path Could someone please explain why the first two...
49
by: Martin Unsal | last post by:
I'm using Python for what is becoming a sizeable project and I'm already running into problems organizing code and importing packages. I feel like the Python package system, in particular the...
6
by: HMS Surprise | last post by:
I imported a set of functions from a file I wrote to interpreter shell: from myFile import * Now if I change functions in this file how can I make python forget it so I can force a fresh...
0
by: Gary Herron | last post by:
Dan Yamins wrote: Because loading (and reloading) assigns values to variables (called binding a value in Python), but does not go on a hunt to find variables to *unbind*. Once a variable is...
0
by: Gabriel Genellina | last post by:
En Fri, 13 Jun 2008 20:01:56 -0300, Dan Yamins <dyamins@gmail.com> escribió: Note that if you execute dir() at this point, you'll see the Operations name, *not* Operations.archive. The...
0
by: Gabriel Genellina | last post by:
En Sat, 18 Oct 2008 05:52:04 -0300, Stef Mientki <stef.mientki@gmail.com> escribió: Why don't you let the caller tell you its own location, using __file__? The above code is too much magic for...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.