import reloading

Robin Becker robin at
Wed Sep 24 15:34:30 CEST 2014

On 24/09/2014 12:54, Dave Angel wrote:
> Robin Becker <robin at> Wrote in message:
>> Is a loader supposed to reset all the reused module's attributes and reset the
>> __dict__ or does it just run the code from the module in hte __dict__?
> Nothing gets reset or run. It simply reuses the existing module object.
I find that a bit weird. In fact as a test I created a

print('A=%r' % A)

then in python3.4

Python 3.4.0 (v3.4.0:04f714765c13, Mar 16 2014, 19:24:06) [MSC v.1600 32 bit 
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
 >>> import testmod
 >>> testmod.A
 >>> #I externally changed to read
 >>> #a=4
 >>> #print('a=%r' % a)
 >>> import imp
 >>> imp.reload(testmod)
<module 'testmod' from 'C:\\code\\tests\\'>
 >>> testmod.A
 >>> testmod.a

so you are right the old module variables are still there. Presumably any 
functions/classes etc etc that don't get overwritten will also continue to exist.

> There are ways to fool the loader, for example by having a module
>   visible in more than one file system path.
> The most common of
>   these bugs  is a module that tries to import the script that's
>   started it all. Because that initial script is named __main__
>   internally,  if you try to import it by filename you end up with
>   two instances.

Robin Becker

More information about the Python-list mailing list