472,096 Members | 2,272 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,096 software developers and data experts.

command-line arguments in IDLE

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.

Nov 7 '07 #1
2 16206
On Nov 7, 6:27 am, "Russ P." <Russ.Paie...@gmail.comwrote:
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

Nov 7 '07 #2
Russ P. wrote:
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[i]+s[-1] for i,s in enumerate(sorted(m))],
[[chr(154-ord(c)) for c in '.&-&,l.Z95193+179-']]*18)[3]

Nov 7 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Hugh Macdonald | last post: by
8 posts views Thread by Siemel Naran | last post: by
8 posts views Thread by Charles Law | last post: by
4 posts views Thread by FluffyCat | last post: by
reply views Thread by czerwww | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.