Using switches with exec?

Francis Avila francisgavila at yahoo.com
Mon Jan 12 14:47:07 EST 2004


Premshree Pillai wrote in message ...
> --- "Diez B. Roggisch" <nospam-deets at web.de> wrote: >
>> I need to run a Python program dynamically within
>> > another program. I am using exec for the purpose.
>> Is
>> > there a way to pass parameter switches to exec?
>>
>> You can pass a globals-dictionary to exec, which can
>> hold the switches
>> values.
>>
>> Diez
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>
>Could you please elucidate with a sample script.
>Suppose I need to pass the switch "-w" to the
>dynamically loaded code (using eval()/exec()).
>Any help is appreciated.
>
>-Premshree
>

What do you mean by a parameter switch?  You mean a command-line switch?

If this is what you mean, it seems you're mixing concepts.  If you run the
Python program as an external program, just do so using os.system or
os.popen or such--the fact that it's written in Python makes no difference,
and exec/execfile is not involved. os.system('python mypythonprg -w'), for
example.

If you want to have Python code from one program exposed to another, the
best approach is to write a module which can be used either as a library or
a stand-alone program.  The idiom is to put something like this:

if __name__ == '__main__': main()

at the bottom of your code.  When you run this file alone, main() will be
executed; if you import the file, it won't be.

If you really need to execute external Python code internally (and it's very
rare and special circumstances where you'd want to), use execfile().

Now, if you're using execfile, it is unlikely that the code you're running
would use command line switches--this implies that it's a stand-alone
program, in which case you should either be running it externally (as in
first solution above) or importing it, and providing a mechanism to use it
programmatically.  This is why I say you seem to be mixing concepts.

That said, command line arguments are available via sys.argv, and this is
most likely how the external program accesses them.  You can modify sys.argv
before execfile().

This, however, is a mess, because you clobber the global sys.argv of the
original program.  You need to somehow redirect any access to sys.argv by
the script.  You can do so like this:

>>> sys.argv
['']
>>> class ShadowObject(object):
...     def __init__(self, shadowed, **override):
...         """Shadow attr access to shadowed with override."""
...         self.__dict__.update(override)
...         self.shadowed = shadowed
...     def __getattr__(self, attr):
...         """Called only if attr was not in override."""
...         return getattr(self.shadowed, attr)
...
>>> exec '''print sys.argv          #We get the right one?
... print sys.getdefaultencoding()  #Make sure we're shadowing.
... sys.argv = sys.argv[-1:]        #Test reassignments.
... print sys.argv
... ''' in globals(), {'sys':ShadowObject(sys, argv=['progname','-w'])}
['progname', '-w']
latin-1
['-w']
>>> sys.argv # the real sys.argv is unchanged
['']

Don't do this.  It's almost certainly not the best way to do what you want.
Why don't you tell us your broader task, and maybe we can suggest something
more Pythonic?
--
Francis Avila




More information about the Python-list mailing list