Re: re-importing modules
- From: Carsten Haese <carsten@xxxxxxxxxxx>
- Date: Mon, 30 Apr 2007 16:00:09 -0400
On Mon, 2007-04-30 at 12:44 -0700, kyosohma@xxxxxxxxx wrote:
On Apr 30, 12:49 pm, "T. Crane" <tcr...@xxxxxxxxxxxxxxxxxx> wrote:
Hi,
When troubleshooting code that's saved in a text file, I often find that I
want to make a change to it, re-save it, then reimport it. However, just
typing
import myTestCode
doesn't always seem to import the newer version. Is it supposed to? I find
that right now I often have to close my iPython window, then reopen it and
import my recently modified code. I know this can't be the best way to do
this, but I don't know what is.
any suggestions/help welcome and appreciated,
trevis
Hi,
Another person posted the same thing today. As with that person, you
probably need to use the reload() function. See this post for more
details:
http://www.python.org/search/hypermail/python-1993/0342.html
Mike
In addition to the warning that reload() does not recursively reload
modules that the reloaded module depends on, be warned that reloading a
module does not magically affect any functions or objects from the old
version that you may be holding on to. For example:
Module code:
# dog.py
class dog(object):
def bark(self): print "Arf!"
Interactive session:
Arf!import dog
d = dog.dog()
d.bark()
<module 'dog' from 'dog.py'># Now the module code is being changed in another window...
reload(dog)
Woof!# A new dog instance will now say Woof:
d2 = dog.dog()
d2.bark()
Arf!# But the dog instance from before still says Arf:
d.bark()
This may or may not be a problem for you, but the bottom-line is that
reload must be used with caution.
-Carsten
.
- References:
- re-importing modules
- From: T. Crane
- Re: re-importing modules
- From: kyosohma
- re-importing modules
- Prev by Date: Re: re-importing modules
- Next by Date: Re: Is it possible to merge xrange and slice?
- Previous by thread: Re: re-importing modules
- Next by thread: Is it possible to merge xrange and slice?
- Index(es):
Relevant Pages
|