How to set up Eclipse to develop QGis scripts or plugins

Eclipse software is an Eclipse Foundation project, developed and organized into a set of software sub-projects, to develop an open source software production environment that is open-ended, universal and versatile, relying mainly on Java.

As one of the great success stories of Open Source, Eclipse has become a standard in the development software market, built in by leading software publishers as well as service companies.

You can develop a plugin for QGis with a simple text editor, but if you want to have a real development environment, allowing you to quickly debug your scripts, Eclipse is the best solution.
Where to start?

You must download and install   Eclipse AND   the plugin   PyDev .

To download the latest version of Eclipse   :

https://www.eclipse.org/downloads/download.php?file=/oomph/epp/neon/R/eclipse-inst-win64.exe&mirror_id=17

Run the installation file, selecting the installation “Eclipse IDE for Java Developers”

Once the installation is complete,   Eclipse starts. Then you must install the PyDev plugin for Eclipse.

Go to the menu   Help-> Install New software

Go to “Work with” and click  Add    

Go to  name  and  type   PyDev   and in   location  the download site address:   http://pydev.org/updates , and then click   OK  

Check the box   PyDev,  then  Next   and complete the plugin installation.

Once finished, you must restart Eclipse. You must define the Python interpreter to use:

Go to the menu   Windows-> Preferences 

In the left window, search for the item   PyDev -> Interpreters -> Python Interpreter   then click on New 

Re-enter PyQGIS   as name and point to the file   python.ex e of your QGis installation. It is located in its   bin   directory.

When you click OK, the setup programme researches automatically all the available Python libraries to be added.  If you let this happen, you’re going straight to conflicting libraries. You have to fill in these libraries manually to make sure there is no contradiction.

A warning message appears telling you that no libraries have been specified. Click on   Proceed anyway.

Now, the most boring part of the installation. Be as patient as possible…

We will go back and forth between   Eclipse   and   QGis   to fill in all the necessary libraries.

For each   Path   spotted in   QGis , you will have to click on the button   New Folder   of  Eclipse and point to the directory concerned:

In   QGis   we will use the Python console. Open it and simply type   qgis   . You will get back the PyQGIS API path

Use your insight to decode the answer, knowing that it fits your QGis installation. In this example the beginning is Programs / QgisEssen. Ignore the /./ as well as the last element. Indeed, what you need to fill in Eclipse this last element directory.

In Eclipse click   New Folder   and in the window that pops up, point directory

Click OK. You will see the directory displayed in the list of PYTHONPATH System

Right! But the hardest remains to be done. Type in the Python console

import sys
sys.path

You will have something like this

For each path element you will have to repeat what we have done for the PyQGIS API, except for items you are sure do not interfere with your development, ie those that do not match your username, those that start with \\ Users \ or those which correspond to software environments other than QGis (in this example the path starting with \\ PCI Geomatics).

Once this is done for all paths, it’s not over! It remains to inform the QGis dlls.

In the Eclipse Preferences window, click the Environment tab

Click on New and in the window that opens type PATH in   Name   and in   Value   you will need to enter the list of QGis directories containing binary files, separated by; .

On Windows, take the following line and change the version of QGis to match the version installed on your computer:

Brighton C: \ Program Files \ QGIS; C: \ Program Files \ QGIS
Brighton \ bin; C: \ Program Files \ QGIS Brighton \ apps \ qgis \ bin; C: \ Program
Brighton Files \ QGIS \ apps \ Python27 \ DLLs

The configuration of Eclipse is complete!!

How to debug Python scripts in QGis?

Both   QGIS   and   Eclipse   must be configured for debugging so that both software can communicate.   Eclipse   binds to   QGIS   to give you an overview of the Python scripts running in QGIS. This approach allows you to run scripts in a controlled manner, suspend execution while you inspect the program for bugs when and where they occur.

When you launch a script in QGis, it is hand over to Eclipse to execute the code, which in turn is returned to QGis.

So we have to configure QGis to send the stream to Eclipse and Eclipse to return the results to QGis.

QGis Configuration.

In this step we will add two plugins to QGIS, which allow Eclipse to communicate with QGIS. A plugin,   Plugin Reloader, allows you to reload a QGIS plugin into the memory without restarting QGIS, to run the tests faster. The second plugin,   Remote Debug, links QGIS to Eclipse.
Remote Debug   is an experimental plugin, so you need to make sure the experimental plugins are visible in the list of available QGIS Plugin Manager plugins.

Install both plugins. Take advantage of the opportunity to install, as well,  the HelloWorldPlugin, which will be helpful to test your configuration. Note the plugin’s location:

Eclipse configuration.

Launch   Eclipse

Go to the menu   File-> New-> Project    

Select   General   then   Project   and click on   Next  

Name the project, for example, to test the operation,   HelloWorld Project , then click on the button   Finish .

Select the project you just created, then right-click and   New -> Folder  

 

In the window   New Folder   which pops up, click on the button   Advanced 

Check the box  “Link to alternate location (Linked Folder)”   Then with the button   browse   point to the plugin directory to debug, in our example   HelloWorld , in the directory that you checked during its installation.

 

Configuration Test

To test the configuration, we will place a breakpoint in the script code   HelloWorld , launch it in   QGis , verify the code is on stop in   Eclipse , restart execution in   Eclipse   and see the window appearing in   QGis .

To set the breakpoint:

In   Eclipse, open the HelloWorld.py file by double-clicking on it

Go to the menu   Window -> Perspective -> Open Perspective -> Other … 

In the new window select   debug   and click on   OK   

The “perspective”   Debug type is taken by Eclipse

Look for the first line of the plugin (def hello_world (self) 🙂 and double-click to the left of the line number to place a breakpoint (green bug)

From the PyDev menu, select   Start Debug Server.

Debug Server at port: 5678

In the console (bellow the Eclipse window).

Now, go to QGis . In the   Extensions menu  click on   Remote Debug -> Remote Debug  

The Remote Debugger window opens. A last configuration is necessary.

Be sure that   Debugger   is placed on   PyDev (Eclipse). In   PyDevd path, as its name suggests, indicate where the file    pydevd.py is located.

The easiest way is to search the Windows file manager and copy paste the path of that file. You only have to perform this operation the first time you use Remote Debugger.

Once done, click Connect, the window closes and a message appears in   QGis   indicating you:   Python Debugging Active .

Launch the HeloWorld plugin from the menu   Extension -> HelloWorld -> HelloWorld.

This, usually, results in displaying a message window with the text   Hello World . But here, nothing happens.

Go to Eclipse .

You will verify that the line with the breakpoint is highlighted. This means that the execution of the script is stopped at this point.

To restart the execution, go to the menu   Run-> Restart

Go to QGis. The Hello World window has appeared!

The configuration of your development environment is complete.

Si cet article vous a intéressé et que vous pensez qu'il pourrait bénéficier à d'autres personnes, n'hésitez pas à le partager sur vos réseaux sociaux en utilisant les boutons ci-dessous. Votre partage est apprécié !

Leave a Reply

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