Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When running a python script in IDLE, is there a way to pass in command line arguments (args)?

I'm testing some python code that parses command line input. Is there a way to pass this input in through IDLE? Currently I'm saving in the IDLE editor and running from a command prompt.

I'm running Windows.

like image 722
Ben Gartner Avatar asked Jan 27 '10 17:01

Ben Gartner


People also ask

How do I pass a command line argument in Python IDLE?

IDLE now has a GUI way to add arguments to sys. argv! Under the 'Run' menu header select 'Run... Customized' or just Shift+F5... A dialog will appear and that's it!

How do you pass command prompt in Python?

Method 1 (CMD /K): Execute a command and then remain Now what if you want to execute multiple command prompt commands from Python? If that's the case, you can insert the '&' symbol (or other symbols, such as '&&' for instance) in between the commands.

How do you pass arguments to a Python script?

In Python, arguments are passed to a script from the command line using the sys package. The argv member of sys ( sys. argv ) will store all the information in the command line entry and can be accessed inside the Python script. Python's getopt module can also be used to parse named arguments.


1 Answers

It doesn't seem like IDLE provides a way to do this through the GUI, but you could do something like:

idle.py -r scriptname.py arg1 arg2 arg3 

You can also set sys.argv manually, like:

try:     __file__ except:     sys.argv = [sys.argv[0], 'argument1', 'argument2', 'argument2'] 

(Credit http://wayneandlayne.com/2009/04/14/using-command-line-arguments-in-python-in-idle/)

like image 90
danben Avatar answered Sep 18 '22 17:09

danben