Using command line arguments in Python in IDLE.
I’m not a big fan of integrated development environments (IDEs), but they can be nice to help people get introduced to a new language, or even to programming in general. Processing and Arduino wouldn’t be as easy for new people to use if they didn’t have a decent IDE.
Most useful scripts accept command line arguments to configure their behavior, but sometimes it is difficult to set command line parameters from within an IDE. I was recently working on a python project with a friend living in Florida, new to the world of programming, so I showed him how to use Python’s IDLE interface, a nice IDE for Python work. Unfortunately, there is no way to set command line arguments from IDLE, so our scripts weren’t working in the IDE. I found a way to manually set the command line arguments if a Python script is run from IDLE, but doesn’t interfere if the script is run from the console.
To use, insert this code right after the start of your main-line code, right before you do your parsing and validation of the input parameters. Be sure that the specified arguments below match what your program is expecting.
try:
__file__
except:
sys.argv = [sys.argv[0], 'argument1', 'argument2', 'argument2']