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 programs in eclipse with PyDev. To develop a python program with eclipse, you should first install the PyDev Eclipse plugin, you can read the 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 the eclipse left project navigation panel.
  2. Then right-click the file content in the eclipse right panel. Click Run As —> Run Configurations or Debug As —> Debug Configurations menu item. It will popup the configuration dialog.
  3. In the popup dialog left panel, click the run configuration name under the Python Run menu item. Then click the Arguments tab in the right panel, then you can input the command line arguments in the Program arguments: text area.

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 with the python sys module.

  1. Import python sys module in your python script.
    import sys
  2. Get the command line arguments with 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.