Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Faking out __name__ == __main__

0 views
Skip to first unread message

Ed Leafe

unread,
Oct 31, 2004, 5:05:59 PM10/31/04
to pytho...@python.org
I'm working on creating a generic runtime engine for the Dabo
framework. Right now I'm focusing on Windows, since many of our
potential users are running on that platform. I've got py2exe and Inno
Setup running, so all that is well and good.

My question concerns the ability to generically run scripts as if they
were being run on an installed copy of Python. Many scripts have the
following structure:

if __name__ == "__main__":
method1()
method2()
...etc.
methodN()

When such a script is run with the command 'python myscript.py', the
various methods are called. However, when run with 'daborun
myscript.py', (daborun is the name of the py2exe file), nothing gets
executed, since when the call to 'myscript.py' is made, __name__ is now
set to 'myscript', and the statements after the 'if __name__' test are
never executed.

I have several ways of working around this, such as requiring that all
code after the 'if __name__' test be moved into a 'main()' method, and
having daborun call the main() method, but it would *so* much cooler to
be able to somehow fake out the value of __name__ when the call is made
to the script. Is this something that might be possible, or is it so
fundamental to the internals of Python that it can't be messed with?

___/
/
__/
/
____/
Ed Leafe
http://leafe.com/
http://dabodev.com/

Bengt Richter

unread,
Oct 31, 2004, 5:27:28 PM10/31/04
to

Maybe this will give you a useful idea?

>>> print '%s\n%s%s'% ('-'*40, open('tmain.py').read(), '-'*40)
----------------------------------------
print 'before'
print 'localnames:', locals().keys(), locals()['__name__']
print 'globalnames:', globals().keys(), globals()['__name__']
if __name__ == '__main__':
print 'after'
----------------------------------------
>>> def test():
... d = {'__name__': '__xxxx__'}
... execfile('tmain.py', d)
...
>>> test()
before
localnames: ['__builtins__', '__name__'] __xxxx__
globalnames: ['__builtins__', '__name__'] __xxxx__
>>> def test():
... d = {'__name__': '__main__'}
... execfile('tmain.py', d)
...
>>> test()
before
localnames: ['__builtins__', '__name__'] __main__
globalnames: ['__builtins__', '__name__'] __main__
after

Note that __builtins__ gets injected, so it will be found in the execution context.
If you need the side effects of the execution, you can find them in the dict d (in
this example).
Regards,
Bengt Richter

Ed Leafe

unread,
Oct 31, 2004, 7:38:19 PM10/31/04
to pytho...@python.org
On Oct 31, 2004, at 5:27 PM, Bengt Richter wrote:

> Maybe this will give you a useful idea?

Very useful; thanks! The runtime now runs script files that use the
'if __name__' construct. I would never have thought that you could
modify the local namespace like that.

Josiah Carlson

unread,
Nov 1, 2004, 1:58:49 AM11/1/04
to Bengt Richter, pytho...@python.org, Ed Leafe

bo...@oz.net (Bengt Richter) wrote:
>
> On Sun, 31 Oct 2004 17:05:59 -0500, Ed Leafe <e...@leafe.com> wrote:
> >>> def test():
> ... d = {'__name__': '__xxxx__'}
> ... execfile('tmain.py', d)

I believe it is technically more Pythonic to use __import__ rather than
execfile, if only because you have access to the module itself when you
are done.


- Josiah

Alex Martelli

unread,
Nov 1, 2004, 3:06:09 AM11/1/04
to
Josiah Carlson <jcar...@uci.edu> wrote:

Unfortunately, while __import__ does take parameters giving local and
global namespaces, it doesn't use them in anywhere like the way that
execfile does. In particular, how would you "force __name__" with
__import__, as per this thread's subj? I don't think you can...


Alex

Bengt Richter

unread,
Nov 1, 2004, 2:45:25 PM11/1/04
to

It seems from a quick hack that you can fake it though, by importing a dummy
and filling it:

Python 2.3.2 (#49, Oct 2 2003, 20:02:00) [MSC v.1200 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print '%s\n%s%s' %('-'*40,open('empty.py').read(),'-'*40)
----------------------------------------

----------------------------------------
>>> print '%s\n%s%s' %('-'*40,open('tmain.py').read(),'-'*40)
----------------------------------------
print 'before'
print 'bef localnames:', locals().keys(), locals()['__name__']
print 'bef globalnames:', globals().keys(), globals()['__name__']

glob_before = 'glob_before'
class Before(object):
def m(self): print glob_before, glob_after

if __name__ == '__main__':
glob_after = 'glob_after'
class After(object):
def m(self): print glob_before, glob_after
print 'after'
print 'aft localnames:', locals().keys(), locals()['__name__']
print 'aft globalnames:', globals().keys(), globals()['__name__']
----------------------------------------
>>> import empty
>>> empty.__dict__['__name__']
'empty'
>>> empty.__dict__['__name__'] = '__main__'
>>> empty.__dict__['__name__']
'__main__'
>>> execfile('tmain.py', empty.__dict__)
before
bef localnames: ['__builtins__', '__name__', '__file__', '__doc__'] __main__
bef globalnames: ['__builtins__', '__name__', '__file__', '__doc__'] __main__
after
aft localnames: ['glob_before', '__builtins__', '__file__', 'After', 'glob_after', '__name__', '
__doc__', 'Before'] __main__
aft globalnames: ['glob_before', '__builtins__', '__file__', 'After', 'glob_after', '__name__',
'__doc__', 'Before'] __main__
>>> dir(empty)
['After', 'Before', '__builtins__', '__doc__', '__file__', '__name__', 'glob_after', 'glob_before']
>>> empty.Before().m()
glob_before glob_after
>>> empty.After().m()
glob_before glob_after
>>> empty.glob_before
'glob_before'
>>> empty.__name__='tmain'
>>> empty.__file__ = r'c:\pywk\clp\tmain.py'
>>>
>>> help(empty)
Help on module tmain:

NAME
tmain

FILE
c:\pywk\clp\tmain.py

DATA
glob_after = 'glob_after'
glob_before = 'glob_before'

>>> type(empty)
<type 'module'>

I haven't really explored all the ramifications ;-)

Regards,
Bengt Richter

0 new messages