Connecting Tech Pros Worldwide Forums | Help | Site Map

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

tony.ha@philips.com
Guest
 
Posts: n/a
#1: Nov 15 '06
Hello,

I have open a Python program in the IDLE, but when I select the "run
module" under "run" menu, it
does not allow me to pass an argument to my Python program!
How do you pass an argument to a Python program under the IDLE? Thanks for
you help!




Joshua.R.English@gmail.com
Guest
 
Posts: n/a
#2: Nov 15 '06

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




On Nov 15, 7:56 am, tony...@philips.com wrote:
Quote:
Hello,
>
I have open a Python program in the IDLE, but when I select the "run
module" under "run" menu, it
does not allow me to pass an argument to my Python program!
How do you pass an argument to a Python program under the IDLE? Thanks for
you help!
I'm using Python on an old Mac with the IDE. I think my solution would
work.

opt_parser = OptionParser()

def myfunc(argstring):
(self.options,self.args) =
sub_opt_parser.parse_args(argstring.split())
f not self.args: self.args = "[default arguments]".split()
...

if __name__=="__main__":
myfunc(argstring)


This means editing the file every time. Since I'm on a Mac, I can also
use:
if __name__=="__main__":
from EasyDialogs import AskString
args = AskString('Options and Arguments')

myfunc(args)

I'm not sure how easy this is to implement on other platforms

Josh English
Joshua.R.English@gmail.com
http://www.spiritone.com/~english

TonyHa
Guest
 
Posts: n/a
#3: Nov 17 '06

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


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.

Joshua.R.English@gmail.com wrote:
Quote:
On Nov 15, 7:56 am, tony...@philips.com wrote:
Quote:
Hello,

I have open a Python program in the IDLE, but when I select the "run
module" under "run" menu, it
does not allow me to pass an argument to my Python program!
How do you pass an argument to a Python program under the IDLE? Thanks for
you help!
>
I'm using Python on an old Mac with the IDE. I think my solution would
work.
>
opt_parser = OptionParser()
>
def myfunc(argstring):
(self.options,self.args) =
sub_opt_parser.parse_args(argstring.split())
f not self.args: self.args = "[default arguments]".split()
...
>
if __name__=="__main__":
myfunc(argstring)
>
>
This means editing the file every time. Since I'm on a Mac, I can also
use:
if __name__=="__main__":
from EasyDialogs import AskString
args = AskString('Options and Arguments')
>
myfunc(args)
>
I'm not sure how easy this is to implement on other platforms
>
Josh English
Joshua.R.English@gmail.com
http://www.spiritone.com/~english
thunderfoot@gmail.com
Guest
 
Posts: n/a
#4: Nov 17 '06

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



TonyHa wrote:
Quote:
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

Fredrik Lundh
Guest
 
Posts: n/a
#5: Nov 17 '06

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


"TonyHa" wrote:
Quote:
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?
one would think that there would be a "set argument" command somewhere, but I
sure couldn't find it.

as a workaround, to help with testing, you can simply do:

import sys

if not sys.argv[1:]:
sys.argv += ["argument1", "argument2", "argument3"]

at the top of your program.

</F>



Closed Thread