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

Arnaud Delobelle arnodel at
Mon Nov 3 20:30:34 CET 2008

TP <Tribulations at Paralleles.invalid> writes:

> Hello,
> I have a script that uses the "optparse" package to parse the command line.
> For example:
> $ --help
> # displays help about
> Is this possible to call such a script with execfile('') once in the Python
> interactive shell?
>>>> execfile( '' )
> 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 in command
> line as well as with execfile?

Have you tried setting sys.argv manually?


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

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


More information about the Python-list mailing list