command-line arguments in IDLE

R

Russ P.

Is it possible to pass command-line arguments when running a program
in IDLE? The "Run" menu does not seem to provide that option. Thanks.
 
T

thunderfoot

Is it possible to pass command-line arguments when running a program
in IDLE? The "Run" menu does not seem to provide that option. Thanks.

Can't you just fake the command line args by setting sys.argv? This
isn't too sophisticated, but it appears to work:

import sys

try:
__file__
except:
sys.argv = ['scriptname.py', 'this','is','a','test','within','idle']

for arg in sys.argv:
print arg

Running from within IDLE:
scriptname.py
this
is
a
test
within
idle

Running from the command prompt:

C:\Python24>python.exe mystuff/argtest.py this is yet another test
mystuff/argtest.py
this
is
yet
another
test

C:\Python24>

HTH,
Don
 
T

Tal Einat

Russ said:
Is it possible to pass command-line arguments when running a program
in IDLE? The "Run" menu does not seem to provide that option. Thanks.

thunderfoot's workaround should work well, but requires changing the
script.

If you want IDLE environment, but don't mind running IDLE from the
command line, you can do the following:

idle.py -r scriptname.py this is a test

The script will run inside IDLE's shell with sys.argv set as you would
expect. The output will go to IDLE's shell, and once the script is
done running the shell will become interactive.

(idle.py is usually found in your Python installation under Lib/
idlelib/)

- Tal Einat
reduce(lambda m,x:[m+s[-1] for i,s in enumerate(sorted(m))],
[[chr(154-ord(c)) for c in '.&-&,l.Z95193+179-']]*18)[3]
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top