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

Discussion in 'Python' started by TP, Nov 3, 2008.

  1. TP

    TP Guest

    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?

    Thanks

    Julien

    --
    python -c "print ''.join([chr(154 - ord(c)) for c in '*9(9&(18%.9&1+,\'Z
    (55l4('])"

    "When a distinguished but elderly scientist states that something is
    possible, he is almost certainly right. When he states that something is
    impossible, he is very probably wrong." (first law of AC Clarke)
    TP, Nov 3, 2008
    #1
    1. Advertising

  2. TP <> 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
    Arnaud Delobelle, Nov 3, 2008
    #2
    1. Advertising

  3. TP schrieb:
    > 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?



    Don't use execfile. Make script.py like this:


    ....

    def main(argv=None):
    if argv is None: argv = sys.argv[1:]
    ...


    Then just do

    import script
    script.main(arguments)


    instead.

    Diez
    Diez B. Roggisch, Nov 3, 2008
    #3
  4. On Nov 4, 12:43 am, "Diez B. Roggisch" <> wrote:
    > def main(argv=None):
    >      if argv is None: argv = sys.argv[1:]
    >      ...


    Wouldn't that make optparse miss the first parameter sys.argv[1]
    mistaking it to be the name of the current program?

    - Sandip
    Sandip Bhattacharya, Nov 4, 2008
    #4
  5. TP

    Tim Chase Guest

    Sandip Bhattacharya wrote:
    > On Nov 4, 12:43 am, "Diez B. Roggisch" <> wrote:
    >> def main(argv=None):
    >> if argv is None: argv = sys.argv[1:]
    >> ...

    >
    > Wouldn't that make optparse miss the first parameter sys.argv[1]
    > mistaking it to be the name of the current program?


    Nope...optparse uses argv[1:] as documented at [1]. The "prog"
    argument can be specified in the constructor to OptionParser, but
    defaults to sys.argv[0] if it's not been explicitly specified.[2]

    -tkc


    [1]
    http://www.python.org/doc/2.5.2/lib/optparse-parsing-arguments.html

    [2]
    http://www.python.org/doc/2.5.2/lib/optparse-creating-parser.html
    (at the "prog" entry at the bottom)
    Tim Chase, Nov 4, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. KathyB
    Replies:
    0
    Views:
    990
    KathyB
    Jun 25, 2003
  2. Brian Blais

    execfile error exception line number?

    Brian Blais, Feb 23, 2006, in forum: Python
    Replies:
    1
    Views:
    341
    =?iso-8859-1?B?QW5kcuk=?=
    Feb 24, 2006
  3. Rahul
    Replies:
    4
    Views:
    538
    Robert Kern
    Apr 7, 2009
  4. Lauren
    Replies:
    4
    Views:
    169
    Lauren
    Mar 14, 2008
  5. neo
    Replies:
    5
    Views:
    89
Loading...

Share This Page