By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,941 Members | 1,789 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,941 IT Pros & Developers. It's quick & easy.

command-line arguments in IDLE

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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.