Passing command line argument to program from within IDLE?

Discussion in 'Python' started by Alan Biddle, Feb 4, 2010.

  1. Alan Biddle

    Alan Biddle Guest

    Just finishing my first Python (2.6 on Win XP) program, which is
    working fine. My "Duh?" question is about how to run it from within
    IDLE and pass it command line arguments. No problem using sys.argv
    from a Windows command line, but I have missed how you can do that
    from within IDLE, which complicates development and debugging.



    --
    Alan
    Alan Biddle, Feb 4, 2010
    #1
    1. Advertising

  2. Alan Biddle

    Terry Reedy Guest

    On 2/4/2010 3:55 PM, Alan Biddle wrote:
    > Just finishing my first Python (2.6 on Win XP) program, which is
    > working fine. My "Duh?" question is about how to run it from within
    > IDLE and pass it command line arguments. No problem using sys.argv
    > from a Windows command line, but I have missed how you can do that
    > from within IDLE, which complicates development and debugging.


    I presume you mean edit, F5-run, see result in shell window.
    Set sys.argv in test function or __name__=='__main__'
    In 3.1 idle shell:

    >>> import sys
    >>> sys.argv

    ['']
    >>> sys.argv = ['abc','dev']
    >>> sys.argv

    ['abc', 'dev']

    I did not know it was writable, either, until I tried it.

    Terry Jan Reedy
    Terry Reedy, Feb 4, 2010
    #2
    1. Advertising

  3. Alan Biddle

    Steve Holden Guest

    Terry Reedy wrote:
    > On 2/4/2010 3:55 PM, Alan Biddle wrote:
    >> Just finishing my first Python (2.6 on Win XP) program, which is
    >> working fine. My "Duh?" question is about how to run it from within
    >> IDLE and pass it command line arguments. No problem using sys.argv
    >> from a Windows command line, but I have missed how you can do that
    >> from within IDLE, which complicates development and debugging.

    >
    > I presume you mean edit, F5-run, see result in shell window.
    > Set sys.argv in test function or __name__=='__main__'
    > In 3.1 idle shell:
    >
    >>>> import sys
    >>>> sys.argv

    > ['']
    >>>> sys.argv = ['abc','dev']
    >>>> sys.argv

    > ['abc', 'dev']
    >
    > I did not know it was writable, either, until I tried it.
    >

    As a solution, however, that sucks, wouldn't you agree?

    regards
    Steve
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    PyCon is coming! Atlanta, Feb 2010 http://us.pycon.org/
    Holden Web LLC http://www.holdenweb.com/
    UPCOMING EVENTS: http://holdenweb.eventbrite.com/
    Steve Holden, Feb 4, 2010
    #3
  4. Alan Biddle

    Alan Biddle Guest

    Yes, that is what I was trying to do. I need to puzzle a bit on the
    solution, being a newbie. Thanks!

    --
    Alan
    Alan Biddle, Feb 4, 2010
    #4
  5. Alan Biddle

    Alan Biddle Guest

    Terry,

    CLICK, the light just came on. Knowing that it is writable, I can
    look at the length to determine whether there are any arguments. If
    not, I can switch to interactive input within the program, and input
    the values that way. A few easy extra lines. Whatever works.
    Thanks!!

    --
    Alan
    Alan Biddle, Feb 4, 2010
    #5
  6. On Thu, 04 Feb 2010 16:28:17 -0500, Steve Holden wrote:

    > Terry Reedy wrote:
    >> On 2/4/2010 3:55 PM, Alan Biddle wrote:
    >>> Just finishing my first Python (2.6 on Win XP) program, which is
    >>> working fine. My "Duh?" question is about how to run it from within
    >>> IDLE and pass it command line arguments. No problem using sys.argv
    >>> from a Windows command line, but I have missed how you can do that
    >>> from within IDLE, which complicates development and debugging.

    >>
    >> I presume you mean edit, F5-run, see result in shell window. Set
    >> sys.argv in test function or __name__=='__main__' In 3.1 idle shell:
    >>
    >>>>> import sys
    >>>>> sys.argv

    >> ['']
    >>>>> sys.argv = ['abc','dev']
    >>>>> sys.argv

    >> ['abc', 'dev']
    >>
    >> I did not know it was writable, either, until I tried it.
    >>

    > As a solution, however, that sucks, wouldn't you agree?


    [scratches head]

    Do you mean setting sys.argv as a solution sucks? No, I don't, I think it
    is grand. If sys.argv was unmodifiable, *that* would suck.

    Or do you mean that trying it as a solution to the problem of answering
    the OP's question sucks? Well, no, experimentation is good for answering
    these sorts of questions, and I can't assume that the documentation will
    cover every imaginable use-case, or that users will find it. In the
    absence of any documentation stating otherwise, I would have assumed that
    sys.argv was an ordinary list which you can modify at will, but having
    been caught out on faulty assumptions before, I would try it and see
    before commenting publicly.


    --
    Steven
    Steven D'Aprano, Feb 4, 2010
    #6
  7. Alan Biddle

    Steve Holden Guest

    Steven D'Aprano wrote:
    > On Thu, 04 Feb 2010 16:28:17 -0500, Steve Holden wrote:
    >
    >> Terry Reedy wrote:
    >>> On 2/4/2010 3:55 PM, Alan Biddle wrote:
    >>>> Just finishing my first Python (2.6 on Win XP) program, which is
    >>>> working fine. My "Duh?" question is about how to run it from within
    >>>> IDLE and pass it command line arguments. No problem using sys.argv
    >>>> from a Windows command line, but I have missed how you can do that
    >>>> from within IDLE, which complicates development and debugging.
    >>> I presume you mean edit, F5-run, see result in shell window. Set
    >>> sys.argv in test function or __name__=='__main__' In 3.1 idle shell:
    >>>
    >>>>>> import sys
    >>>>>> sys.argv
    >>> ['']
    >>>>>> sys.argv = ['abc','dev']
    >>>>>> sys.argv
    >>> ['abc', 'dev']
    >>>
    >>> I did not know it was writable, either, until I tried it.
    >>>

    >> As a solution, however, that sucks, wouldn't you agree?

    >
    > [scratches head]
    >
    > Do you mean setting sys.argv as a solution sucks? No, I don't, I think it
    > is grand. If sys.argv was unmodifiable, *that* would suck.
    >
    > Or do you mean that trying it as a solution to the problem of answering
    > the OP's question sucks? Well, no, experimentation is good for answering
    > these sorts of questions, and I can't assume that the documentation will
    > cover every imaginable use-case, or that users will find it. In the
    > absence of any documentation stating otherwise, I would have assumed that
    > sys.argv was an ordinary list which you can modify at will, but having
    > been caught out on faulty assumptions before, I would try it and see
    > before commenting publicly.
    >
    >

    What I meant was it sucks that IDLE has no way to fill in sys.argv as a
    part of its "Run Module" functionality - something that is present in
    both PythonWin and Wing IDE, for example. But then I have come to the
    conclusion that IDLE doesn't fit my brain, or my brain doesn't fit it.

    That sys.argv is mutable simply reflects the fact that it's a list.

    regards
    Steve
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    PyCon is coming! Atlanta, Feb 2010 http://us.pycon.org/
    Holden Web LLC http://www.holdenweb.com/
    UPCOMING EVENTS: http://holdenweb.eventbrite.com/
    Steve Holden, Feb 4, 2010
    #7
  8. Alan Biddle

    John Bokma Guest

    Steven D'Aprano <> writes:

    > On Thu, 04 Feb 2010 16:28:17 -0500, Steve Holden wrote:
    >
    >> Terry Reedy wrote:
    >>> On 2/4/2010 3:55 PM, Alan Biddle wrote:
    >>>> Just finishing my first Python (2.6 on Win XP) program, which is
    >>>> working fine. My "Duh?" question is about how to run it from within
    >>>> IDLE and pass it command line arguments. No problem using sys.argv
    >>>> from a Windows command line, but I have missed how you can do that
    >>>> from within IDLE, which complicates development and debugging.
    >>>
    >>> I presume you mean edit, F5-run, see result in shell window. Set
    >>> sys.argv in test function or __name__=='__main__' In 3.1 idle shell:
    >>>
    >>>>>> import sys
    >>>>>> sys.argv
    >>> ['']
    >>>>>> sys.argv = ['abc','dev']
    >>>>>> sys.argv
    >>> ['abc', 'dev']
    >>>
    >>> I did not know it was writable, either, until I tried it.
    >>>

    >> As a solution, however, that sucks, wouldn't you agree?

    >
    > [scratches head]
    >
    > Do you mean setting sys.argv as a solution sucks? No, I don't, I think it
    > is grand. If sys.argv was unmodifiable, *that* would suck.
    >
    > Or do you mean that trying it as a solution to the problem of answering
    > the OP's question sucks? Well, no, experimentation is good for answering
    > these sorts of questions, and I can't assume that the documentation will
    > cover every imaginable use-case, or that users will find it. In the
    > absence of any documentation stating otherwise, I would have assumed that
    > sys.argv was an ordinary list which you can modify at will, but having
    > been caught out on faulty assumptions before, I would try it and see
    > before commenting publicly.


    I guess that Terry means that a solution that makes it possible to
    specify in IDLE *outside* of the Python code the arguments would be
    better. Hardcoding the command line arguments isn't something I would do
    for testing.

    --
    John Bokma j3b

    Hacking & Hiking in Mexico - http://johnbokma.com/
    http://castleamber.com/ - Perl & Python Development
    John Bokma, Feb 4, 2010
    #8
  9. Alan Biddle

    John Bokma Guest

    John Bokma <> writes:

    > Steven D'Aprano <> writes:
    >
    >> On Thu, 04 Feb 2010 16:28:17 -0500, Steve Holden wrote:
    >>
    >>> Terry Reedy wrote:
    >>>> On 2/4/2010 3:55 PM, Alan Biddle wrote:
    >>>>> Just finishing my first Python (2.6 on Win XP) program, which is
    >>>>> working fine. My "Duh?" question is about how to run it from within
    >>>>> IDLE and pass it command line arguments. No problem using sys.argv
    >>>>> from a Windows command line, but I have missed how you can do that
    >>>>> from within IDLE, which complicates development and debugging.
    >>>>
    >>>> I presume you mean edit, F5-run, see result in shell window. Set
    >>>> sys.argv in test function or __name__=='__main__' In 3.1 idle shell:
    >>>>
    >>>>>>> import sys
    >>>>>>> sys.argv
    >>>> ['']
    >>>>>>> sys.argv = ['abc','dev']
    >>>>>>> sys.argv
    >>>> ['abc', 'dev']
    >>>>
    >>>> I did not know it was writable, either, until I tried it.
    >>>>
    >>> As a solution, however, that sucks, wouldn't you agree?

    >>
    >> [scratches head]
    >>
    >> Do you mean setting sys.argv as a solution sucks? No, I don't, I think it
    >> is grand. If sys.argv was unmodifiable, *that* would suck.
    >>
    >> Or do you mean that trying it as a solution to the problem of answering
    >> the OP's question sucks? Well, no, experimentation is good for answering
    >> these sorts of questions, and I can't assume that the documentation will
    >> cover every imaginable use-case, or that users will find it. In the
    >> absence of any documentation stating otherwise, I would have assumed that
    >> sys.argv was an ordinary list which you can modify at will, but having
    >> been caught out on faulty assumptions before, I would try it and see
    >> before commenting publicly.

    >
    > I guess that Terry means that a solution that makes it possible to
    > specify in IDLE *outside* of the Python code the arguments would be
    > better. Hardcoding the command line arguments isn't something I would do
    > for testing.


    Oops, that should've been Steve, my apologies.

    --
    John Bokma j3b

    Hacking & Hiking in Mexico - http://johnbokma.com/
    http://castleamber.com/ - Perl & Python Development
    John Bokma, Feb 4, 2010
    #9
  10. En Thu, 04 Feb 2010 20:46:52 -0300, John Bokma <>
    escribió:
    > John Bokma <> writes:


    >>>>> On 2/4/2010 3:55 PM, Alan Biddle wrote:
    >>>>>> Just finishing my first Python (2.6 on Win XP) program, which is
    >>>>>> working fine. My "Duh?" question is about how to run it from within
    >>>>>> IDLE and pass it command line arguments.


    >> I guess that Terry means that a solution that makes it possible to
    >> specify in IDLE *outside* of the Python code the arguments would be
    >> better. Hardcoding the command line arguments isn't something I would do
    >> for testing.

    >
    > Oops, that should've been Steve, my apologies.


    See http://bugs.python.org/issue5680

    --
    Gabriel Genellina
    Gabriel Genellina, Feb 5, 2010
    #10
  11. Alan Biddle

    Terry Reedy Guest

    On 2/4/2010 6:29 PM, Steve Holden wrote:
    > Steven D'Aprano wrote:
    >> On Thu, 04 Feb 2010 16:28:17 -0500, Steve Holden wrote:
    >>
    >>> Terry Reedy wrote:
    >>>> On 2/4/2010 3:55 PM, Alan Biddle wrote:
    >>>>> Just finishing my first Python (2.6 on Win XP) program, which is
    >>>>> working fine. My "Duh?" question is about how to run it from within
    >>>>> IDLE and pass it command line arguments. No problem using sys.argv
    >>>>> from a Windows command line, but I have missed how you can do that
    >>>>> from within IDLE, which complicates development and debugging.
    >>>> I presume you mean edit, F5-run, see result in shell window. Set
    >>>> sys.argv in test function or __name__=='__main__' In 3.1 idle shell:
    >>>>
    >>>>>>> import sys
    >>>>>>> sys.argv
    >>>> ['']
    >>>>>>> sys.argv = ['abc','dev']
    >>>>>>> sys.argv
    >>>> ['abc', 'dev']
    >>>>
    >>>> I did not know it was writable, either, until I tried it.
    >>>>
    >>> As a solution, however, that sucks, wouldn't you agree?


    No, see below.

    >> [scratches head]
    >>
    >> Do you mean setting sys.argv as a solution sucks? No, I don't, I think it
    >> is grand. If sys.argv was unmodifiable, *that* would suck.
    >>
    >> Or do you mean that trying it as a solution to the problem of answering
    >> the OP's question sucks? Well, no, experimentation is good for answering
    >> these sorts of questions, and I can't assume that the documentation will
    >> cover every imaginable use-case, or that users will find it. In the
    >> absence of any documentation stating otherwise, I would have assumed that
    >> sys.argv was an ordinary list which you can modify at will, but having
    >> been caught out on faulty assumptions before, I would try it and see
    >> before commenting publicly.
    >>
    >>

    > What I meant was it sucks that IDLE has no way to fill in sys.argv as a
    > part of its "Run Module" functionality - something that is present in
    > both PythonWin and Wing IDE, for example.


    The first thing I did was to check for such an option on the run tab.
    The second thing I thought of was to suggest to OP that he file a
    feature request (which would likely take years, if ever). But then the
    test line would not be fetched from the file (unless some ##cmdline
    convention were invented), but would have to be hand-entered each time
    the editor were run. Also, a given run of a program can have only one
    command line, and changing the run line for each test case would suck.
    Then I tried sys.argv.

    Why that does not suck:

    A Python program, as far as I know, cannot process a command lines
    directly, but only processed command line args in the form of a list of
    strings -- sys.argv. Therefore, the need for testing is not to simulate
    a command line but to set sys.argv. I assume the programmer knows the
    mapping from command line to argv since he otherwise cannot sensibly
    proceed.

    I believe it is sensible, if not recommended, to put an arg processing
    in a defined function that returns, for instance, a dict, rather than to
    do it at top-level in the main script. So in use, process_args() will be
    called in a context with sys.argv set. So let it be tested. For
    instance, something like:

    def _test_arg_processing:
    arg_dict_pairs - (
    (['arg1', 'arg2'], {'speed':int('arg1'), 'range':float('arg2'}),
    (['-v'], {'verbose': True},
    )
    for args,dic in arg_dict_pairs:
    sys.argv = args
    assert process_args() == dic

    Repeatable automatic testing of multiple test cases.
    I just starting doing this a year ago and love it.

    Terry Jan Reedy
    Terry Reedy, Feb 5, 2010
    #11
  12. Alan Biddle

    Steve Holden Guest

    Gabriel Genellina wrote:
    > En Thu, 04 Feb 2010 20:46:52 -0300, John Bokma <>
    > escribió:
    >> John Bokma <> writes:

    >
    >>>>>> On 2/4/2010 3:55 PM, Alan Biddle wrote:
    >>>>>>> Just finishing my first Python (2.6 on Win XP) program, which is
    >>>>>>> working fine. My "Duh?" question is about how to run it from within
    >>>>>>> IDLE and pass it command line arguments.

    >
    >>> I guess that Terry means that a solution that makes it possible to
    >>> specify in IDLE *outside* of the Python code the arguments would be
    >>> better. Hardcoding the command line arguments isn't something I would do
    >>> for testing.

    >>
    >> Oops, that should've been Steve, my apologies.

    >
    > See http://bugs.python.org/issue5680
    >

    So we've had a patch since April and it still didn't make it into 3.1?
    That's disappointing. I wonder if there's any chance it'll get into 2.7.

    regards
    Steve
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    PyCon is coming! Atlanta, Feb 2010 http://us.pycon.org/
    Holden Web LLC http://www.holdenweb.com/
    UPCOMING EVENTS: http://holdenweb.eventbrite.com/
    Steve Holden, Feb 5, 2010
    #12
  13. Alan Biddle

    Tim Chase Guest

    Gabriel Genellina wrote:
    > En Thu, 04 Feb 2010 20:46:52 -0300, John Bokma <>
    >> Oops, that should've been Steve, my apologies.

    >
    > See http://bugs.python.org/issue5680



    Am I the only one that expected that issue to be about too many
    Steves (and perhaps too many Tims) on c.l.p? :)

    -tkc
    Tim Chase, Feb 5, 2010
    #13
  14. On Thu, 04 Feb 2010 19:36:19 -0600, Tim Chase wrote:

    > Gabriel Genellina wrote:
    >> En Thu, 04 Feb 2010 20:46:52 -0300, John Bokma <>
    >>> Oops, that should've been Steve, my apologies.

    >>
    >> See http://bugs.python.org/issue5680

    >
    >
    > Am I the only one that expected that issue to be about too many Steves
    > (and perhaps too many Tims) on c.l.p? :)



    That was exactly my thought too.


    One of the multiple Steves'ly yrs,


    --
    Steve
    Steven D'Aprano, Feb 5, 2010
    #14
  15. Alan Biddle

    John Bokma Guest

    Tim Chase <> writes:

    > Gabriel Genellina wrote:
    >> En Thu, 04 Feb 2010 20:46:52 -0300, John Bokma
    >> <>
    >>> Oops, that should've been Steve, my apologies.

    >>
    >> See http://bugs.python.org/issue5680

    >
    > Am I the only one that expected that issue to be about too many Steves
    > (and perhaps too many Tims) on c.l.p? :)


    Ha ha ha, yeah something like that, wasn't expecting what was actually
    on that page.

    --
    John Bokma j3b

    Hacking & Hiking in Mexico - http://johnbokma.com/
    http://castleamber.com/ - Perl & Python Development
    John Bokma, Feb 5, 2010
    #15
    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. Fong
    Replies:
    3
    Views:
    1,156
  2. Hayri ERDENER

    command line argument passing

    Hayri ERDENER, Jul 22, 2005, in forum: Python
    Replies:
    3
    Views:
    395
    Dennis Benzinger
    Jul 22, 2005
  3. Replies:
    4
    Views:
    569
    Fredrik Lundh
    Nov 17, 2006
  4. virgincita schmidtmann

    passing command line argument to array

    virgincita schmidtmann, May 4, 2007, in forum: C Programming
    Replies:
    6
    Views:
    454
    Old Wolf
    May 6, 2007
  5. kenny roytman
    Replies:
    0
    Views:
    136
    kenny roytman
    Oct 30, 2007
Loading...

Share This Page