[Django]-Virtualenv with Eclipse (Galileo)

17👍

I’d disagree with having to go through all the hassle of creating and maintaining a separate workspace for every virtual environment.

All you need to do is set up a separate interpreter per virtualenv and make sure the project is using it.

Along with your standard interpreters such as Python 2.5, Python 2.6, Python 3.1 you’ll also add some more along the lines My Django Website, My Cool Project, My Other Cool Project–where each interpreter will have all the PYTHONPATH entries as it’s virtualenv would provide.

12👍

What problems are you having?
The key is having separate workspaces for each project. Then select the python interpreter for that workspace to the one created for the virtualenv. Then you should be set.

11👍

My instructions for creating a Django Virtual Environment which works with Eclipse are as follows;

Note: The instructions are for OSX Mountain Lion, but should work with other operating systems. I have collated this information from various sources and would appreciate any suggestions or comments. I will assume you have python, virtualenv and eclipse set up on your system.

Open a terminal, move to the location you would like to have your eclipse workspace and;

  • mkdir projectenv
  • cd project env
  • virtualenv venv –distribute
  • source venv/bin/activate

Now, lets install the dependencies;

  • pip install Django psycopg2 dj-database-url (Your needs may vary from mine)

Now we will start the Django project and commit to git;

  • django-admin.py startproject myproject
  • pip freeze > requirements.txt
  • git init; git add; git commit -m myproject (Please have a .gitignore file with venv and *pyc in it before doing this step)

Our django project is set up and ready to go, so now open eclipse and at the workspace selector, click browse and select the projectenv folder (i.e. the folder which contains the venv folder, the myproject folder and the requirements.txt folder) and click open.

Go to File, Import, General, Existing Folder as New Project and select the myproject folder, click finish. Your project will now appear in the package explorer – you should now switch to the PyDev perspective if not already on it.

Right click on the main myproject folder in the package explorer, go down to PyDev and select ‘Set as PyDev project’. Eclipse will now prompt you to set up the interpreter and will take you to the preferences window. Click New, and select the interpreter in /venv/bin/ select python, not python2.7 and click ok.

You will get a list of libs, leave them as they are and click finish, you will get a warning, but click proceed anyway.

Now, click on New Folder in the bottom half of the prefs window and select /venv/lib/, click ok, then click apply, then click ok.

Finally, right click on manage.py and Run As, Run Configurations. In the Arguments tab, type;

  • runserver –noreload

then click Apply and then Close.

That should be that, when you want to add an app, do so on the command line as you normally would using manage.py startapp myapp (if you install the Aptana Studio plugin, you can get a terminal window inside eclipse), right click the main project folder in eclipse and hit refresh, everything will be there. When you want to debug, set your breakpoints, hit Debug As python manage.py (the config you set up earlier) and when you hit a code breakpoint, Eclipse fires you into the debug perspective.

I find this gives me the perfect mix, it means I can write a lot of stuff on the command line as normal, but because it’s set up in Eclipse, when things aren’t going my way, I can fire up eclipse and do some real debugging!

I hope this helps.

4👍

Not sure about Galileo since I have upgraded to Helios.

It’s easy to setup Project->PyDev – Interpreter/Grammar -> Interpreter based on different projects. When configure the interpreter to point to virtualenv’s python interpreter, Pydev doesn’t automatically inherit the system python’s path, therefore it’s the user’s duty to select appropriate PYTHONPATH. But you can always go back to edit that in Preferences->Pydev->Interperter – Python -> Libraries tab.

👤Dingle

3👍

Based on the information here (and others found when I was trying to solve the same problem you had), I put together a post with step-by-step instructions here.

The short answer, as the Doctor says, is to make each virtualenv correspond to a workspace – so when you create a new one of the former, you create a new one of the latter exclusively to use with it.

👤Dan

Leave a comment