February 1, 2007

PyDEV uses Eclipse's power to program in Python

Author: Nikos Kouremenos

PyDEV is a plugin that enables developers to use Eclipse for Python and Jython development, making Eclipse a first-class Python IDE. The software offers features such as code completion, syntax highlighting, syntax analysis, automated refactoring, template system, source code quality checking, and a debugging environment.

To begin using PyDEV, you first need to have Eclipse and Python installed. Both Java, the language Eclipse is written in, and Python run on most common platforms, including Windows, Linux/Unix, and Mac OS X, so you can work with PyDEV on just about any OS. To get started, from inside Eclipse, click Help -> Software Updates -> Find and Install. Choose Search for new features for install and then click New Remote Site.... Call it PyDev, and in the URL section type http://pydev.sf.net/updates/

One of PyDEV's great strengths is its features for debugging Python source code. Until I used PyDEV I never truly debugged a Python application, because I didn't like using the Python debugger module. Instead, I would just add print statements to the code and try to follow the logic to catch the bugs. While this works in many cases, sometimes you wish you knew what value a variable was holding and you don't feel like adding yet another print statement, or you would like to avoid going into a function call -- stepping over in debug jargon.

You can do all that and do it visually with the PyDEV debugger. It provides a debugging environment where you can dynamically add and remove breakpoints and navigate through all the values of variables or even the properties and attributes of instances. Or to quickly find the value of a variable, just select the variable and press Ctrl-Shift-D. Unfortunately you can't dynamically assign a value to a variable -- right click -> Change Value... won't work.

The lines in Python traceback output are converted to hyperlinks, which you can click and directly be led to the problematic line. This saves developers a lot of time.

Refactoring also becomes easy because of the integration of Bicycle Repair Man in PyDEV. Bicycle Repair Man provides automated refactoring functionality to Python. With it, to move a section of a code to a new method, just highlight the lines, right-click, and choose Refactoring -> Extract Method. Variables are automatically detected in the extracted code and automatically passed as arguments in the new method.

With PyDEV's Tasks Panel, a single click gives you an interactive overview of all your pending tasks, so you no longer have to run grep TODO on your files, manually open them, and go to lines in question. To specify tags so your lines are marked as Tasks, add them in Window -> Preferences -> Pydev -> Task Tags.

Eclipse is famous for its templates -- source code fragments you can create and afterwards use while coding. PyDEV also supports them. For example, type def and press Ctrl-Space and a small window with predefined matches will appear. To add your templates or customize the predefined ones, go to Window -> Preferences -> Pydev -> Code Completion -> Templates.

You can also use Pylint in Eclipse via PyDEV to clean up your code, though it is disabled by default. With it you can catch typos and bad cut-pastes, make sure you adhere to coding standards, and in general be notified of signs of poor code quality. PyDEV also supports PyUnit for those that are interested in doing unit testing.

Other provided features that can make your coding easier include content assistance and some automations of the editor. For content assistance, press Ctrl-1 while you're typing and a set of options will appear to choose from: Move "import module" line to global scope, Create docstring, Assign result (from a method you are calling in the same line) to new local variable, and Surround code with try..except or try..finally -- the most useful option of all.

The PyDEV editor is smart about indentation. If you write an if statement, the next line will be automatically indented, along with the rest of the code section below it, until you type pass or elif or else. Moreover, if you are inside a class and you define a method, the editor will automatically insert self as first argument to the method. Similarly, if you start typing from module, import will appear right after, making it faster to write from module import name statements.

PyDEV is stable and new versions are released often. For even more features, have a look at the proprietary PyDEV Extensions package, which adds to the open source PyDEV plugin.

For more information, Fabio Zadrozny, the main developer of PyDEV, offers demonstration videos about PyDEV and PyDEV Extensions.

Nikos Kouremenos is a computer science student who has been active in the open source community since 2002.

Category:

  • Python
Click Here!