sys.modules and __main__ obscureness

Fuzzyman fuzzyman at gmail.com
Sun Jul 16 15:11:24 EDT 2006


K.S.Sreeram wrote:
> Fuzzyman wrote:
> > This code behaves differently when entered into an interactive
> > interpreter session. When run as a program you will see that module and
> > namespace have both become None !!
>
> Thats because the reference count to the current '__main__' module goes
> to 0, and the module object gets deleted along with its globals 'module'
> and 'namespace'. The code keeps working because the codeobject is
> retained by the interpreter.
>
> To make it work, simply retain a reference to the existing module,
> before overwriting with the new module.
>
> sys.blahblah = sys.modules['__main__']
> sys.modules['__main__'] = module
>
> [sreeram;]

That was a quick response. :-)

Thanks very much.

Fuzzyman
http://www.voidspace.org.uk/python/index.shtml

>
>
> --------------enigF3B976014D166081569F1C89
> Content-Type: application/pgp-signature
> Content-Disposition: inline;
> 	filename="signature.asc"
> Content-Description: OpenPGP digital signature
> X-Google-AttachSize: 253




More information about the Python-list mailing list