All Unkept
Posted in: Python, Django  —  January 02, 2010 at 10:08 PM

Eclipse, PyDev, Django and virtualenv

by Luke Plant

I'm just trying Eclipse + PyDev, to see if I can improve on my trusted Emacs. In the past, I've struggled to get IDEs to work well with virtualenv etc. — not that Emacs has been better, but I need positive advantages if I'm going to switch.

But I have a solution I'm happy with:

  1. Create a new workspace for each virtualenv environment. Setup the Python interpreter to be the one from that environment (Window → Preferences → PyDev → Interpreter - Python) . You will then have to select the paths to be included — it lists everything in your *.pth files, but doesn't seem to select everything by default. In particular, external directories (rather than unpacked eggs within the site-packages dir) that I had added to easy-install.pth were not selected. I'm guessing if I add more things into the virtualenv environment I will need to manually select them in Eclipse as well.

    This isn't quite perfect integration with virtualenv, but it is relatively painless.

  2. Create a project in Eclipse for your project. I hate the Eclipse wanting to put my actual source within the project directory, but thankfully you can use links - make sure you deselect the option to add a "src" directory. Then, right click on the project and choose "New -> Folder", and select "Advanced" to be able to add a link to an existing project. This basically works the same a symlink, but avoids some of the problems that symlinks can cause (in particular, settings.py files that refer to relative paths outside the project structure).

  3. In the project settings, add the project source directory (the symlink just created) to the PYTHONPATH.

With this in place, PyDev seems to find all my code just fine. I can press 'F3' on almost any name and get to the source, and I have get code completion for most things, whether internal to my project, builtin libraries or external libraries.

You can then setup a Run/Debug configuration to do "manage.py runserver". Note the following things:

  • You need to specify "--noreload" as an argument
  • Specify not to launch in background
  • Make sure that your 'manage.py' is not on the PYTHONPATH. I had some issues with this, and it doesn't need to be on the PYTHONPATH as you are specifically selecting it to run.

After these are in place, I can do full debugging from Eclipse, with easy setting of breakpoints etc. Which is pretty awesome actually.

If you need to debug your dependencies (e.g. Django itself), add them as source folders instead and remove them from the PYTHONPATH of the interpreter.

You can also switch to Emacs key bindings, which works nicely. I'm already missing lots of Emacs things, like:

  • "everything is a buffer" - so help text appears in a buffer, allowing you to do incremental search on it etc. Using help in Eclipse is going to be painful.
  • keyboard macros
  • customising things via a .emacs file
  • modes for everything, including reStructuredText, and being able to activate different modes at will.

But despite these things, this is the first time in a while that I've tried an IDE with benefits that might outweigh the losses for Python programming.

It's worth backing up your workspace to a tarball once you have got this into a working state. I found that installing Aptana actually broke my debugging (it produces a bizarre "Error: That port is already in use."), but uninstalling Aptana didn't fix it, and it took me a while to get back to a working state.

Comments §

blog comments powered by Disqus