Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing command Line argument to Python script within Eclipse(Pydev)

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 !

like image 830
newprint Avatar asked Dec 04 '10 20:12

newprint


People also ask

How do we pass command line arguments in Eclipse?

To specify command line arguments in eclipse, go to Run -> Run… Make sure you are running the correct project for which you want to specify command line arguments for, and then select the arguments tab. Now enter the arguments you want, separated by spaces.

How do I run Python in PyDev?

Configure PyDevGo to Window → Preferences. In the Preferences window, expand PyDev and select Interpreter-Python. Click "New..." and type Python32 for the Interpreter name. For the Interpreter executable, browse to your copy of Python (C:\Program Files\Python32\python.exe), and press Open.


4 Answers

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

like image 61
Andrew White Avatar answered Oct 27 '22 02:10

Andrew White


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,

like image 33
Blue Peppers Avatar answered Oct 27 '22 04:10

Blue Peppers


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.

like image 24
BenH Avatar answered Oct 27 '22 04:10

BenH


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 .

like image 41
denis Avatar answered Oct 27 '22 04:10

denis