Re: re-importing modules



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:
import dog
d = dog.dog()
d.bark()
Arf!
# Now the module code is being changed in another window...
reload(dog)
<module 'dog' from 'dog.py'>
# A new dog instance will now say Woof:
d2 = dog.dog()
d2.bark()
Woof!
# But the dog instance from before still says Arf:
d.bark()
Arf!

This may or may not be a problem for you, but the bottom-line is that
reload must be used with caution.

-Carsten


.



Relevant Pages

  • Re: re-importing modules
    ... When troubleshooting code that's saved in a text file, ... want to make a change to it, re-save it, then reimport it. ... probably need to use the reload() function. ...
    (comp.lang.python)
  • re-importing modules
    ... When troubleshooting code that's saved in a text file, ... want to make a change to it, re-save it, then reimport it. ...
    (comp.lang.python)