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