How To Pass Command Line Parameters To Python Script In Eclipse Pydev

In this article, i will tell you how to pass command line arguments to python script program in eclipse with pydev. To develop python program with eclipse, you should first install pydev eclipse plugin, you can read article How To Run Python In Eclipse With PyDev.

1. Pass Command Line Arguments To Python Script In Eclipse Steps.

  1. Select the python script file ( it is test_sys_argv.py in this example. ) in eclipse left project navigation panel.
    select python script file in eclipse
  2. Then right click the file content in eclipse right panel. Click Run As —> Run Configurations or Debug As —> Debug Configurations menu item. It will popup the configuration dialog.
    eclipse - run as - run configurations
  3. In the popup dialog left panel, click the run configuration name under Python Run menu item. Then click Arguments tab in right panel, then you can input the command line arguments in the Program arguments: text area.
    eclipse - debug configurations - add command line arguments

2. Get The Command Line Arguments Value In Python Script.

Now when you run or debug your python script, you can get the command line arguments use python sys module.

  1. Import python sys module in your python script.
    import sys
  2. Get the command line arguments use sys.argv, it will return an array. Below is the example source code.
    # get python script command line arguments length.
    len = len(sys.argv)
    
    cmd = 'create'
    
    # loop the argument array.    
    for i in range(len):
        # print each argument and it's value.    
        print('arg ',i ,' : ', sys.argv[i])
        
    # get needed command line argument value.
    if len > 1:
        cmd = sys.argv[1]

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.