Posted by & filed under GeoDjango.

Eclipse is a Java IDE right? Wrong! You can use it for other languages too! I have been needing to improve my debugging techniques for python and Django (more on that in the future). This post outlines how you can set up Eclipse with PyDev to work on an existing Django aplication. The benifits? A complete IDE environment with a debugger that will let you set breakpoints anywhere in your app and interactively step through the code from that point.  You also get other nice things like refactoring support, code completion and so on. I’m still a dyed in the wool command line user since I can use my same development environment both locally and on a remote server effortlessly,  but for training courses and those who prefer to use a GUI, PyDev on Eclipse will probably make a compelling alternative. lets see how to get is set up (after the break below)…

These steps are also explained in the PyDev documentation which is quite good. First you need to do Help->Install New Software. Then click on the ‘Add…’ button and fill in the details as shown below:

Add the PyDev extensions (click for larger image)

Add the PyDev extensions (click for larger image)

Next choose the PyDeb and PyDev Extensions item from the ‘work with’ list and wait a moment (PyDeb in the screenshots is a typo in my part  – it should read ‘PyDev’).  Then tick ‘PyDev’ from the software list and untick ‘Contact all update sites to find required software’. Then click next and wait while the packages download. It took a few attempts to work for me on our slow internet connection. After you are done, click finish to close the installer and restart Eclipse as prompted.

Choose the PyDev source and select PyDev from the list (click for larger image).

Choose the PyDev source and select PyDev from the list (click for larger image).

I’m going to assume that you have an existing Django web site you want to work with, but note that the PyDev will also happily create a new Django project from scratch for you. I’m also assuming you prefer to work with python virtual environments since you are not a crazy nutter right? Right, good so here are the steps I followed for an existing project:

  •  create a new django project in your Eclipse project workspace named the same as your existing project
  • remove the project director and replace it with a symlink to your real project
  • setup Eclipse to be aware of our virtualenv
  • set breakpoints, run the development server and generally have fun with Django within an IDE
So first up open Eclipse and use a workspace (I took the default one as shown below):
Open a workspace (click for larger image)

Open a workspace (click for larger image)

Next, create a new Django project: File -> New -> Project…. and then choose PyDev Django Project from the dialog that appears.

Create a new django project (click for larger image)

Create a new django project (click for larger image)

Click next and then you should give the project the same name as the existing Django project that you want to bring into Eclipse (in my case greatcircle). Also choose ‘don’t configure PYTHONPATH’ from the options in the dialog that appears:

Configure your project (click for larger version)

Configure your project (click for larger version)

 

Use the hyperlink on the above dialog to configure your python interpreter. On the screen that appears we get to tell PyDev where our virtualenvironment is. Click New…then fill the interpreter details in like this:

Point pydev to your virtual environment (click for larger version)

Point pydev to your virtual environment (click for larger version)

 

When you click OK, a dialog like this will appear, and you should enable the system python (‘/usr/lib/python2.7′ in my case) too.

Configured python interpreter dialog (click for larger view)

Configured python interpreter dialog (click for larger view)

Clicking OK will take  you back to the Eclipse preferences window and should look something like this:

Final view of Eclipse preferences (click for larger view)

Final view of Eclipse preferences (click for larger view)

Now click OK and it will start doing some magic stuff, eventually bringing you back to the ‘PyDev Django Project’ dialog. Be sure to change the interpreter option to ‘GreatCircle VirtualEnvironment’ (or similar based on your previous choices). Click next and you will be prompted for your database connection details. You can completely ignore this since we will be grafting in the settings from our existing ‘greatcircles’ project below. So clicking Finish will end this part of the process.

At this point you should completely close Eclipse and carefully run the following commands (substituting details for your own project of course):

cd ~/workspace/greatcircle/
rm -rf greatcircle
ln -s /home/web/greatcircle/greatcircle greatcircle

Basically, the commands just substitute the PyDev generated stub project with a symbolic link to the real project. Now we can open up Eclipse again and open our project. You may need to press F5 to refresh the project view before you see all your real project files. Good so now we have our project all set up in Eclipse it should look something like this:

 

Our django project loaded in Eclipse (click for larger image)

Our django project loaded in Eclipse (click for larger image)

 

The last part of this article covers debugging. The process is really simple.  First open a source file (for example views.py) and then double click in a margin where you would like to place a break point.

Setting a breakpoint in your project (click for larger image)

Setting a breakpoint in your project (click for larger image)

 

Next you can run the django development server  by right clicking on the project and from the context menu choosing Debug As -> PyDeb: Django.

Launching the debugger (click for larger image)

Launching the debugger (click for larger image)

 

PyDev will prompt you to switch to the debug perspective. Now you can go ahead and open the site in your browser. When you hit the url that triggers the breakpoint, PyDev will stop, highlight the line and you can use the normal debugging tools for there on. You can view the state of any variable while django is running and so on.

 

Debugging your Django application interactively - woohoo! (Click for larger image)

Debugging your Django application interactively - woohoo! (Click for larger image)

 

From my limited usage, it doesn’t quite have the power of my traditional pdb command line debugging environment. For example being able to change variables at runtime and so on. It does however offer the convenience of not needing to insert extra code into your sources to trigger breakpoints.

Eclipse feels big and unwieldy to me after using my svelte command line environment for this kind of thing, but it does have some compelling features and makes a great platform for training people who are command line averse. I’m going to revisit PyDev again in a future article to see how well it works as a platform for writing and debugging QGIS plugins.

 

 

  • fabiofz

    Hi there,

    Just wanted to share my 2c on the debugger:

    When you hit a breakpoint, you can use the console to issue any python command (see: http://pydev.org/manual_adv_debug_console.html ), so, you can change variables, make prints, etc (so, it should be at least as flexible as pdb, but with an UI so that you can follow better what’s happening).

    And for changing a variable you may even right-click it in the variables view and select ‘change variable’ to change it (although you can probably also do it in the console directly — the only thing — and that’s a python limitation — is that you can only change it at the topmost frame).

    Note that depending on your environment, you may also want to use the remote debugger: http://pydev.org/manual_adv_remote_debugger.html

    Cheers,

    Fabio

    • http://linfiniti.com Tim Sutton

      Hi

      Ah thanks Fabio. I will go and try out these features!

      Regards

      Tim

  • Pingback: Apfel trifft Schlange – Python auf dem Mac | aflx - always flexible