Faking out __name__ == __main__

E

Ed Leafe

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/
 
B

Bengt Richter

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?
Maybe this will give you a useful idea?
----------------------------------------
print 'before'
print 'localnames:', locals().keys(), locals()['__name__']
print 'globalnames:', globals().keys(), globals()['__name__']
if __name__ == '__main__':
print 'after'
---------------------------------------- ... d = {'__name__': '__xxxx__'}
... execfile('tmain.py', d)
... before
localnames: ['__builtins__', '__name__'] __xxxx__
globalnames: ['__builtins__', '__name__'] __xxxx__ ... d = {'__name__': '__main__'}
... execfile('tmain.py', d)
... 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
 
J

Josiah Carlson

... 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
 
A

Alex Martelli

Josiah Carlson said:
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.

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
 
B

Bengt Richter

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...
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 '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 'module'>

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

Regards,
Bengt Richter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top