24

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 !

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
newprint
  • 6,936
  • 13
  • 67
  • 109

5 Answers5

59

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:

enter image description here

Franck Dernoncourt
  • 77,520
  • 72
  • 342
  • 501
Andrew White
  • 52,720
  • 19
  • 113
  • 137
10

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,

Blue Peppers
  • 3,718
  • 3
  • 22
  • 24
  • 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. – newprint Dec 04 '10 at 21:46
2

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.

BenH
  • 2,100
  • 2
  • 22
  • 33
1

Years later, and not Eclipse, but a variant of other answers to run my.py M=11 N=None ... in sh or IPython:

import sys

# parameters --
M = 10
N = 20
...

# to change these params in sh or ipython, run this.py  M=11  N=None ...
for arg in sys.argv[1:]:
    exec( arg )
...
myfunc( M, N ... )

See One-line-arg-parse-for-flexible-testing-in-python under gist.github.com/denis-bz .

denis
  • 21,378
  • 10
  • 65
  • 88
-1

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 :(

Vineet
  • 624
  • 1
  • 11
  • 26