All Unkept
Posted in: Django, Python  —  2 January 2010

Eclipse, PyDev, Django and virtualenv

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 §

§ On 25 May 2010, Igor Ganapolsky wrote: 880
How is symlinking different than creating a new PyDev project from the source folder?

§ On 2 August 2010, David wrote: 924
Error: That port is already in use.

Can be fixed by a restart. It means that a previous instance of your app is still holding on to the port (eg, 8080).

A quick way to continue debugging is to pass a port number (8081) to runserver. Then use http://localhost:8081 to view your app.

Ex:
./manage.py runserver 8081

§ On 2 August 2010, Anon wrote: 925

I tried using Restructured text and it failed...


§ On 11 September 2010, Guil wrote: 932
I also got the error
Error: That port is already in use.

In my case I have Aptana installed and discovered that it launches a preview server on port 8000. To stop it, I opened the "Servers" view from the Aptana perspective, selected the server and stopped it.


§ On 15 November 2010, Brandon Taylor wrote: 956
I'm not sure why you'd need to create a new workspace per virtualenv. I have one workspace, and set the Python Path per project simply using the Properties menu on each project in the Project Explorer view.

Here's my process:

1. Load whatever virtualenv I need.
2. Create a new project using django-admin.py in my /home/django_projects folder or import the project from svn.
3. Import the project into Aptana.
4. Right-click on the project and set it as a PyDev project.
5. Right click on the project and edit the PythonPath property, specifying my virtualenv's site-packages and my /home/django_projects folders as external resources.

From that point, I have code-completion for all of the modules in my virtualenv and my project. Being able to set the Python Path per-project, in addition to creating Python runtimes for debugging is what makes Aptana/Eclipse + PyDev an awesome choice for Django or Python development.

Aptana has outstanding code-completion support for HTML, CSS, JavaScript and many JavaScript libraries on top of Ruby, Rails, PHP and Python. It's been my editor of choice for about 3 years.

Happy coding!

§ On 15 November 2010, Rock Howard wrote: 958
Brandon,

Thanks for the notes. I would like this pinned down a bit more for couple of steps:

1) Load whatever virtualenv I need.
   I take this to mean that you create a virtualenv via the shell as per normal.
3) Import the project into Aptana.
   What option? File System? Existing projects into Workspace?

Also were you able to integrate this with subclipse or some other svn integration?

§ On 11 December 2010, Ashu wrote: 969
yeah that is the preview server that for the tip .

§ On 10 May 2011, Dirk Bergstrom wrote: 1038
If you're coming to Eclipse from Emacs, you really need to know about Emacs+:

http://www.mulgasoft.com/emacsplus/

Gives you almost every emacs-ism you can think of inside of Eclipse.

Add comment

Format:

  • Javascript has to be on to get past my spam protection, and cookies, and there is a delay, sorry for any inconvenience!
  • I reserve the right to moderate comments.