Eclipse with the PyDev module has a lot to offer the Python programmer these days. If you haven't looked at PyDev before, or not in a while, it's worth checking out.
Here are some of my favorite features:
- One-keystroke navigation to the definitions of variables, methods, classes
- Code completion, including automatically adding import statements
- Clean up imports
- Refactoring, including renaming across projects
- Clean up whitespace
There are many more. I recommend taking a look at the PyDev web site and blog to see what might appeal to you.
Getting Eclipse and PyDev
If you're already using Eclipse, you can add PyDev to it. If not, you also have the option to get a version of Eclipse with PyDev already included. You install PyDev into your existing Eclipse the same way you install any other Eclipse add-on: first tell Eclipse where to find the add-on, then install it.
- In Eclipse 3.6 and 3.7, select Help/Install New Software...
- On the panel that pops up, click "Add..." at the top right.
- Enter any name (e.g. "PyDev")
- Enter http://pydev.org/updates as the Location, then click OK.
- In the list of available software, select PyDev.
- Click Next, Next, accept the license, Finish.
- If Eclipse asks whether to trust the PyDev certificate, agree.
- When the install is complete, allow Eclipse to restart.
To get Eclipse with PyDev already installed, go to http://www.aptana.com/products/studio3/download and download Aptana Studio for your platform. Aptana Studio 3.0.4 is Eclipse 3.6 plus PyDev plus other add-ons.
Preferences
There are some preferences in Eclipse you probably want to change if you'll be working with Python. Open the preferences by selecting Window/Preferences, then use search to find and set these:
- Insert spaces for tabs: checked, but note that the PyDev editor ignores this and you need to make a similar setting in the PyDev settings for editing Python files.
- Show whitespace characters:
- In Eclipse 3.6, you probably want this off except when you're looking for trailing whitespace.
- In Eclipse 3.7, you can check the box and then click on "whitespace characters" and set just the trailing whitespace visible, which is unobtrusive enough to leave enabled all the time.
- Replace tabs with spaces when typing: checked. This is the one that PyDev obeys.
- Right trim lines: checked, otherwise you end up with a lot of lines with just indentation on them.
- Add newline at end of file: checked.
- Auto-Format editor contents before saving: If you check this, every time you save a file PyDev will fix it to comply with the other settings on this preferences page. That's great if you're working on your own project, but not so good if you're doing maintenance on somebody else's project and don't want to make random changes to white-space all over the place.
Explore the other PyDev settings. The "Code Analysis" section is particularly interesting, as it lets you control the kinds of things that Pydev marks as errors or warnings.
Finally, at least one Python interpreter needs to be configured. Still in Preferences, go to PyDev/Interpreter - Python. For now, just click "Auto Config" and click OK on the dialog that pops up. Then click OK to close Preferences. PyDev will take a while to analyze the python installation and libraries.
Perspective
Select Window/Open Perspective/Other and choose PyDev.
Starting to use Eclipse and PyDev with a project
I typically use Eclipse with Django projects, though I haven't tried PyDev's Django-specific features yet.
When I want to work with a project in Eclipse, first I check it out locally. Then here are the steps I follow:
- File/New/Project (not PyDev project, I don't like the PyDev new project wizard)
- Choose General/Project, click Next
- Enter a project name
- Uncheck "use default location" and set the location to the top directory of my project
- Click Finish
- Right-click on the project and select PyDev/Set as Pydev Project
- Right-click on the project and select Properties
- go to PyDev - PYTHONPATH
- In the Source Folders tab, use "Add source folder" to add folders that need to be on your python path for your project to work. Often this is either the top-level project folder or a folder immediately inside it.
Using PyDev with virtualenv
If you use virtualenv (and if not, why not?), there are a couple additional steps to take.
First, add the interpreter from your virtual environment as another Python interpreter:
- Open Preferences
- Go to PyDev/Interpreter - Python
- Click "New..."
- For the Executable, navigate to your virtual environment's bin directory and select the Python interpreter there.
- Choose another name for your interpreter if you want, probably something shorter than the default. I like to use the name of the virtual environment, with "-env" appended.
- Click OK
- Now here's the tricky part - a dialog will pop up asking which library folders to add. Keep the defaults but you also need to add your system python library directories - e.g. /usr/lib/python2.6, /usr/lib64/python2.6, and /usr/lib/python2.6/plat-linux. Otherwise PyDev won't be able to find all the libraries your python interpreter will be using.
- Click OK
Then, set the new interpreter as the interpreter for your project:
- Right-click the project and select Properties
- Go to Pydev - Interpreter/Grammar
- Under Interpreter, select your new interpreter
- Click OK
Now PyDev should be able to find any libraries you have installed in the virtual environment when needed.
If you install additional libraries, you might need to go back to the interpreter definitions, click "Apply", and tell Pydev which interpreters it should scan again. Until you do that, PyDev might not notice your new libraries.
For more information, see http://pydev.blogspot.com/2010/04/pydev-and-virtualenv.html
Links
- PyDev web site: http://pydev.org/
- PyDev blog: http://pydev.blogspot.com/
- Aptana Studio download: http://www.aptana.com/products/studio3/download