script that parses command line, and execfile('')

Arnaud Delobelle arnodel at googlemail.com
Mon Nov 3 14:30:34 EST 2008


TP <Tribulations at Paralleles.invalid> writes:

> Hello,
>
> I have a script that uses the "optparse" package to parse the command line.
> For example:
>
> $ script.py --help
> # displays help about script.py
>
> Is this possible to call such a script with execfile('') once in the Python
> interactive shell?
>
>>>> execfile( 'script.py' )
>
> I get errors because there is no argv dictionary when used with execfile.
>
> How to solve this problem, so that I am able to use script.py in command
> line as well as with execfile?

Have you tried setting sys.argv manually?

e.g.

>>> import sys
>>> sys.argv = ['--help']
>>> execfile('script.py')

But I have to say I have never felt the need to use execfile() this way.

-- 
Arnaud



More information about the Python-list mailing list