Passing Command line argument to Python program using IDLE? [duplicate]
Asked Answered
O

2

8

I have downloaded a python file xxxxxx.py that is supposed to run on the command line by typing: python xxxxxx.py filename1 filename2 and that should take these two files as arguments.

I was wondering if there is a way I can use IDLE to pass in these arguments. Is there a way other than setting sys.argv ?

Thanks

Observer answered 2/2, 2011 at 0:16 Comment(6)
I don't understand why people insist on thinking that IDLE is something useful.Faubert
Please, you can give me suggestions on an IDE that you think is useful for windows. I would appreciate thatObserver
You don't pass arguments in IDLE at run time. It's easy to simply type your command at the command line. What stops you from tying python xxxxxx.py filename1 filename2 at the command line? Or copying and pasting it at the command line? Or using up-arrow to type it again at the command line?Clinician
@Clinician how does one debug with breakpoints from the command line? With IDLE you can do that easilyCorner
@Ignacio Vazquez-Abrams: I can't tell for everyone, but to me the main reason is that IDLE is accessible virtually everywhere. This would be my tool of choice to quickly illustrate/present the Python code snippet to someone else. I saw it being used for this very purpose at "PyCons".Folacin
As of 3.7.4 and 3.8.0b2, use Run ... Customized on the Run menu.Coinsure
P
4

You can do this from the command line with:

idle.py -r scriptname.py put arguments here

You can try a different IDE like ActivePython

Or you can patch IDLE:

http://bugs.python.org/issue5680

Pentecost answered 2/2, 2011 at 0:25 Comment(4)
if I am on windows. my path now is C:\Python26...etc \ idlelib so I can run idle. But the script is somewhere else and so it cannot find it, what should I do?Observer
Never Mind! I just added the C:\Python26\Lib\idlelib to the path in the Advanced System SettingsObserver
Just modify sys.argv within your code. For example sys.argv = ['scriptname.py', 'arg1', 'arg2', 'arg3']Migraine
A revision of the patch for issue 5680 is in 3.7.4 and 3.8.b2 as Run => Run Customized.Coinsure
M
6

It depends on the content of your Python file. If it is well-written, like:

#! /usr/bin/env python

def process(files):
   for file in files:
       # ...

if __name__ == '__main__'
    # some error checking on sys.argv
    process(sys.argv[1:])
    sys.exit(0)

Then you could simply import the python file and run it like:

 import name_of_file

 # ...
       name_of_file.process([file1, file2, file3])
 # ...

So, it really depends on how it is written. If it isn't written well but you can edit it, I would refactor it so that it can be used as a library; otherwise, I would use the subprocess module to invoke the program.

Magdalenamagdalene answered 2/2, 2011 at 0:23 Comment(2)
I just want to pass argument on run time when I run the python script. Is there a way to do that?Observer
@SaherAhwal As of 3.7.4 and 3.8.0b2, use Run ... Customized on the Run menu.Coinsure
P
4

You can do this from the command line with:

idle.py -r scriptname.py put arguments here

You can try a different IDE like ActivePython

Or you can patch IDLE:

http://bugs.python.org/issue5680

Pentecost answered 2/2, 2011 at 0:25 Comment(4)
if I am on windows. my path now is C:\Python26...etc \ idlelib so I can run idle. But the script is somewhere else and so it cannot find it, what should I do?Observer
Never Mind! I just added the C:\Python26\Lib\idlelib to the path in the Advanced System SettingsObserver
Just modify sys.argv within your code. For example sys.argv = ['scriptname.py', 'arg1', 'arg2', 'arg3']Migraine
A revision of the patch for issue 5680 is in 3.7.4 and 3.8.b2 as Run => Run Customized.Coinsure

© 2022 - 2024 — McMap. All rights reserved.