up vote 5 down vote favorite
share [g+] share [fb]

I am new to Python & Eclipse, and having some difficulties understanding how to pass command line argument to script running within Eclipse(Pydev).

The following link explains how to pass command line argument to python script.

To pass command line argument to module argecho.py(code from link above),

#argecho.py
import sys

for arg in sys.argv: 1
    print arg

I would need to type into python console

[you@localhost py]$ python argecho.py             
argecho.py

or

[you@localhost py]$ python argecho.py abc def     
argecho.py
abc
def

How would I pass same arguments to Python script within Eclipse(Pydev) ???

Thanks !

link|improve this question

feedback

4 Answers

up vote 3 down vote accepted

If you want your program to ask for arguments interactively, then they cease to be commandline arguments, as such. However you could do it something like this (for debugging only!), which will allow you to interactively enter values that the program will see as command line arguments.

import sys
sys.argv = raw_input('Enter command line arguments: ').split()

#Rest of the program here

Note that Andrew's way of doing things is much better. Also, if you are using python 3.*, it should be input instead of raw_input,

link|improve this answer
What I meant by word interactive is - opening console/shell within Eclipse, and typing commands to execute my script. For example, CTRL+ALT+ENTER lets you choose which console to open. – user465292 Dec 4 '10 at 21:46
feedback

What I do is:

Open the project in debug perspective. In the console, whenever the debugger breaks at breakpoint, you can type python command in the "console" and hit return (or enter). There is no ">>" symbol, so it is hard to discover.

But I wonder why eclipse doesn't have a python shell :(

link|improve this answer
feedback

Select "Properties" -->> "Run/Debug Settings".

Select the related file in right panel and then click on "Edit" button. It will open properties of selected file. There's an "Arguments" tab.

link|improve this answer
feedback

Click on the play button down arrow in the tool bar -> run configurations -> (double click) Python Run -> Arguments tab on the right hand side.

From there you can fill out the Program Arguments text box.

link|improve this answer
Is there way to pass arguments interactively ?? – user465292 Dec 4 '10 at 21:01
Not natively. The next best solution is to read from stdin which is what Blue Peppers is recommending. – Andrew White Dec 4 '10 at 22:53
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.