473,503 Members | 1,739 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sys.modules and __main__ obscureness

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
3 2337
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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1616
by: KN | last post by:
Hello, I'm new to this list, and I have an important question at the beginning. There is a matter that concerns me - it's the globales implementation. I've read nearly everything I found about...
0
1937
by: Nick Coghlan | last post by:
Anyone playing with the CPython interpreter's new command line switch might have noticed that it only works with top-level modules (i.e. scripts that are directly on sys.path). If the script is...
15
2559
by: Nick Coghlan | last post by:
Python 2.4's -m command line switch only works for modules directly on sys.path. Trying to use it with modules inside packages will fail with a "Module not found" error. This PEP aims to fix that...
9
5340
by: Martin Drautzburg | last post by:
My wxPython program starts execution in mainFrame.py like this class MainApp(wxApp): def OnInit(self): self.mainFrame = MainFrame(None) self.mainFrame.Show() self.SetTopWindow(self.mainFrame)...
2
1280
by: PGMoscatt | last post by:
Does Python allow a 'global' variable that can be used between different modules used within an app ? Pete
2
2791
by: Tian | last post by:
I am python beginner, I have a question about the interdependence of modules. For example, when I have two modules: module1.py ------------- def plus(x): return add(x,1)
12
2355
by: qwweeeit | last post by:
The pythonic way of programming requires, as far as I know, to spread a big application in plenty of more manageable scripts, using import or from ... import to connect the various modules. In...
7
2056
by: Jorgen Grahn | last post by:
I have a set of tests in different modules: test_foo.py, test_bar.py and so on. All of these use the simplest possible internal layout: a number of classes containing test*() methods, and the good...
3
1324
by: ttsiodras | last post by:
With a.py containing this: ========== a.py =========== #!/usr/bin/env python import b g = 0 def main(): global g
0
7087
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7281
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7334
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7462
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5014
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3156
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1514
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
737
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.