By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,530 Members | 1,578 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,530 IT Pros & Developers. It's quick & easy.

sys.modules and __main__ obscureness

P: n/a
Hello all,

I am messing with namespaces, so that code I exec thinks it is
executing in the __main__ module.

I have the following code :

import imp
import sys

# can't call the module '__main__' or 'new_module' returns the real one
module = imp.new_module('_')
namespace = module.__dict__
namespace['__name__'] = '__main__'

# next put things into the names
# e.g. :
namespace['variable'] = 3

sys.modules['__main__'] = module

print module
print namespace
import __main__
print __main__.__dict__['variable']

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 !!

However the code works, but to get access to the namespace again I have
to import __main__. I just wondered why ?
All the best,
Fuzzyman
http://www.voidspace.org.uk/python/index.shtml

Jul 16 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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;]
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFEuo1Mrgn0plK5qqURArxlAKCY/AnOz2W0hg408wJ6q4PhbtvoPwCePbTn
EC3vRrcBbaYtSfNdoZy3wVg=
=bzrH
-----END PGP SIGNATURE-----

Jul 16 '06 #2

P: n/a

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
Jul 16 '06 #3

P: n/a
Fuzzyman wrote:
That was a quick response. :-)

Thanks very much.
Sigh.. When I'm drowning in arcane win32 c++ crap, I tend to jump on
anything interesting on python-list. It feels like a breath of fresh air!

[sreeram;]
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFEuqUIrgn0plK5qqURAnoTAJ9aulkTgHFZZZbYbsvyKe lcLP4C3wCdGwUS
WezD9nYW6AsoU/8ZPTU0SDA=
=1r+I
-----END PGP SIGNATURE-----

Jul 16 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.