sys.modules and __main__ obscureness

K.S.Sreeram sreeram at tachyontech.net
Sun Jul 16 15:02:33 EDT 2006


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;]

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 260 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20060717/1a8cc1bc/attachment.sig>


More information about the Python-list mailing list