How to pass an argument to a python program open in IDLE?

thunderfoot at gmail.com thunderfoot at gmail.com
Fri Nov 17 10:02:00 EST 2006


TonyHa wrote:
> Hello Josh,
>
> Thanks for the reply. But I am not sure I understand your reply, may be
> I need to explain my problem a bit more. I have a Python script which
> needs an input argument to run.
> e.g. python myscript.py xilinx. which run fine.
>
> My problem is this: When I start IDLE GUI, then I open my script with
> the edit window. (i.e.
> File -> open). I run my script under the edit window using run -> run
> module or F5. But IDLE does not allow me to input the argument to my
> script, i.e. IDLE runs without prompting for the argument, then my
> script fails. I wonder how can I pass the argument to my script under
> IDLE?
>
> Tony Ha.
>

Just check the number of arguments and prompt if the argument is
missing:

import sys

if __name__ == '__main__':
    numArgs = len(sys.argv)
    if numArgs == 1:
        myArg = raw_input('please supply missing argument: ')
    else:
        myArg = sys.argv[1]

    print 'argument : %s' % myArg




More information about the Python-list mailing list