import overwrites __name__

Glenn Hutchings zondo42 at googlemail.com
Sat May 9 01:16:35 CEST 2009


On Fri, 08 May 2009 19:14:52 +0200, Marco wrote:

> I import a module and then the name space of the importing module seems do 
> be overwritten.
> 
> my_name = __name__
> print my_name
> print len(dir())
> from x import y as z
> print __name__
> print len(dir())
> print my_name
> 
> ->
> __main__
> 119
> x
> 117
> unhandled NameError "name 'my_name' is not defined"

Interesting.  Here's one possibility for the contents of module x.py:

import sys
del sys.modules['__main__'].my_name
y = 42

I can't think of any other way apart from grovelling through sys.modules.

Glenn



More information about the Python-list mailing list