How To Run Python In Eclipse With PyDev

Eclipse is the most popular IDE for java development. A lot of users are familiar with it. Besides the java project, it can also be used to develop Python applications. This article just wants to tell you how to use it to develop Python applications.

1. Install PyDev Eclipse Plugin.

  1. Before you can develop the Python applications, you should install PyDev ( an eclipse plugin ) in eclipse. Below are the two installation methods.

1.1 Install Eclipse PyDev Plugin From Eclipse Marketplace.

  1. Before you can install the Eclipse PyDev plugin, you should make sure you have installed JDK 11 or above and python 2.6 or newer.
  2. Open Eclipse, and click Help —> Eclipse Marketplace… menu item at the top toolbar.
  3. Input PyDev in the Find: search text box and click the Go button to search. Then click the Install button to install it.
  4. The installation process may take several minutes, and during the process, you just need to click Confirm, Next, or Finish button. You should restart Eclipse when the installation is completed.

1.2 Use Eclipse PyDev Plugin With LiClipse.

  1. If you can not install the Eclipse PyDev plugin, you can use LiClipse instead.
  2. LiClipse is a lightweight eclipse editor that has integrated the eclipse Pydev plugin.
  3. You can go to the LiClipse download page to download the version that you need.
  4. Just double-click the installer file to install it.
  5. After installation, it will start the LiClipse automatically, you can find that it is similar to Eclipse.

1.3 Configure Python In Eclipse.

  1. After eclipse restart, if this is the first time you install the PyDev plugin, it will pop up a Python not configured dialog window like below.
    Python not configured
    
       It seems that the Python interpreter is not currently configured.
    
       How do you want to proceed?
    
       Manual-config     Config-first-in-PATH     Choose-from-list    Don't-ask-again
  2. If you click the Choose-from-list button, it may pop up the Unable to auto-config dialog like below.
    Auto-configurer could not find a valid interpreter.
    Please manually configure a new interpreter instead.
    Note: the system environment variables that are used when
    auto-searching for a Jython interpreter are the following:
    — PATH
    — PYTHONHOMEI P\"'THON_HOME
    Reason:
    Errors getting info on discovered interpreter{s).
    See error log for details.
    
                                   OK       Details
  3. Click the OK button to return back to the Python not configured dialog,  then click the Manual config button.
  4. It will open the eclipse Preferences dialog window. It will automatically expand the PyDev —> Interpreters —> Python Interpreter menu item in the dialog left side.
  5. Then you can click the New… button on the right side Python Interpreters area, and it will show a list of buttons including Browse for python/pypy exe, New with Pipenv, Config first in PATHChoose from list, Choose from Conda.
  6. You can click the Browse for python/pypy exe button to browse the python executable file ( python.exe ) that you have installed on your OS.
  7. If you use anaconda, you can open one anaconda virtual environment and open it’s terminal and run the command conda info to get your anaconda installed directory, then you can find the python.exe file in that directory ( How To Find Where Anaconda Python Is Installed ).
    (MyPythonEnv) C:\Users\zhaosong>conda info
    
         active environment : MyPythonEnv
        active env location : C:\Users\zhaosong\anaconda3\envs\MyPythonEnv
                shell level : 1
           user config file : C:\Users\zhaosong\.condarc
     populated config files : C:\Users\zhaosong\.condarc
              conda version : 4.10.1
        conda-build version : 3.20.5
             python version : 3.8.5.final.0
           virtual packages : __win=0=0
                              __archspec=1=x86_64
           base environment : C:\Users\zhaosong\anaconda3  (writable)
          conda av data dir : C:\Users\zhaosong\anaconda3\etc\conda
      conda av metadata url : https://repo.anaconda.com/pkgs/main
               channel URLs : https://repo.anaconda.com/pkgs/main/win-64
                              https://repo.anaconda.com/pkgs/main/noarch
                              https://repo.anaconda.com/pkgs/r/win-64
                              https://repo.anaconda.com/pkgs/r/noarch
                              https://repo.anaconda.com/pkgs/msys2/win-64
                              https://repo.anaconda.com/pkgs/msys2/noarch
              package cache : C:\Users\zhaosong\anaconda3\pkgs
                              C:\Users\zhaosong\.conda\pkgs
                              C:\Users\zhaosong\AppData\Local\conda\conda\pkgs
           envs directories : C:\Users\zhaosong\anaconda3\envs
                              C:\Users\zhaosong\.conda\envs
                              C:\Users\zhaosong\AppData\Local\conda\conda\envs
                   platform : win-64
                 user-agent : conda/4.10.1 requests/2.24.0 CPython/3.8.5 Windows/10 Windows/10.0.19041
              administrator : False
                 netrc file : None
               offline mode : False

2. Create Eclipse Python Project, Package, And Module In PyDev.

  1. After successfully installing the eclipse PyDev plugin, you can now use it to create a python project.

2.1 Create PyDev Python Project In Eclipse.

  1. Open eclipse, and click the menu item File —> New —> Other in the top toolbar. And input the keyword PyDev in the popup dialog Wizards input text box. Then select PyDev Project in the bottom PyDev list, then click the Next button.
  2. Then the PyDev Project settings dialog popup. In this dialog, you should select the project directory, input the project name, and most importantly you need to configure an interpreter for the PyDev project.
    pydev-project-settings-dialog-popup
  3. Just click the blue link under the Interpreter section in the above dialog, the link text is Please configure an interpreter before proceeding. A Configure interpreter dialog will pop up like below.
    configure-pydev-project-interpreter-popup-dialog
  4. Click the Advanced Auto-Config button, it will pop up all the installed python interpreter executable file paths for you to select, just select the one you need then click the OK button.
  5. In the next popup dialog, select all the libraries that you want to add to the system python path. Click the OK button to complete the PyDev project creation.

2.2 Create Python Package With PyDev.

  1. Click File —> New —> Other menu item. Input the keyword PyDev in the Wizards filter text box, and select PyDev P&ackage in the PyDev-related items list window below the filter text box.
  2. In the next Create a new Python package dialog, browse to select a Source Folder, input a package Name, and click the Finish button.
  3. Now you can see that the package structure files have been created in the left PyDev Package Explorer panel. There is an __init__.py file in each package, you can write package initialization code in this file. The below picture also contains a Login.py file, this is not the package default file, it is just a module file I created, I will introduce it later.
    pydev-project-package-and-module-files-list-panle

2.3 Create Python Module With PyDev.

  1. Click File —> New —> Other menu item again.
  2. Select PyDev —> PyDev &Module wizard item at this time.
  3. In the Create a new Python module wizard dialog, browse to select the module package Source Folder and input the Package (com.dev2qa) and module Name (Login), please note the module name is case sensitive. Click the Finish button, then you can see the Login.py file on the left side PyDev Package Explorer file list.

2.4 Open Exist PyDev Project In Eclipse.

  1. Open eclipse, then click File —> Open Projects from File System.. menu item.
  2. Click the Directory… button in the popup window, and select the PyDev project exist directory.
  3. Click the Finish button in the above window, then you may meet the below popup dialog to let you configure the python interpreter.
             Python not configured
    It seams that the Python interpreter is not currently configured.
    
    How do you want to proceed?
    
    Cunfig first in PATH    Choose from list   Don't ask again    Manual config
    
  4. Click the Choose from list button in the above dialog. Then select the python version that you want to use and click the OK button.

3. Run And Debug Python Code In Eclipse Using PyDev.

  1. Now the login python module has been created, you can copy the below python code in it.
  2. Login.py
    def login(* usernamepassword):
        username = usernamepassword[0]
        password = usernamepassword[1]
    
        if(username=='hello') and (password == 'hello'):
            print("Login Success!")
        else:
            print("Login Failed!")
    
    if __name__ == '__main__':
        login('hello', 'hello')

3.1 Run Python Code In Eclipse.

  1. Right-click the python file source code, click Run As —> Python Run menu item, then the python file will be executed. You can see the output in the bottom console window.

3.2 Debug Python Code In Eclipse.

  1. Double-click the column before the line number in the source code editor area to set a break point.
  2. Then right-click the python file source code, click Debug As —> Python Run menu item to run the python source code in debug mode. When it runs to the breakpoint, the execution will be paused, you can then watch variable values and debug the code.
  3. This example demo video URL is How To Run Python In Eclipse With PyDev.

2 thoughts on “How To Run Python In Eclipse With PyDev”

  1. ExampleProject\retryeample\logonin.py”, line 7
    username = usernamepassword[]
    ^
    SyntaxError: invalid syntax

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.

Clicky