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:
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.
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
Next, create a new Django project: File -> New -> Project…. and then choose PyDev Django Project from the dialog that appears.
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:
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:
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.
Clicking OK will take you back to the Eclipse preferences window and should look something like this:
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:
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.
Next you can run the django development server by right clicking on the project and from the context menu choosing Debug As -> PyDeb: Django.
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.
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.