Virtualenv with Eclipse (Galileo)

15,562

Solution 1

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.

Solution 2

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.

Solution 3

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.

Solution 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.

Solution 5

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.

Share:
15,562

Related videos on Youtube

Adam Nelson
Author by

Adam Nelson

Technology leader experienced in leading teams and growing startups from nothing. Now working at Alexa.

Updated on August 07, 2020

Comments

  • Adam Nelson
    Adam Nelson almost 4 years

    Does anybody have directions for getting Eclipse (Galileo), PyDev, and Virtualenv working together? I'm specifically trying to run Pinax but any instructions are fine.

    I thought I had it (and even blogged everything but the final step - interactive debugging) and still there is no solution. I'm specifically on OS X but any answer should be sufficient. This is the best resource I've found so far:

    http://blog.vlku.com/index.php/2009/06/10/djangoeclipse-with-code-complete-screencast/

    • Nathan Campos
      Nathan Campos almost 15 years
      I'm having some troubles with Eclipse plug-ins to: stackoverflow.com/questions/1150072/…
    • Nathan Campos
      Nathan Campos almost 15 years
      In my answer i put all the link that can help you. Thanks!
  • Adam Nelson
    Adam Nelson almost 15 years
    Ahh, that's a good idea. I just got swamped right now and can't confirm it - hopefully tonight.
  • Adam Nelson
    Adam Nelson about 14 years
    I just moved to TextMate and said good bye to Eclipse. Any thoughts on whether Eclipse is worth it if set up properly?
  • Filip Dupanović
    Filip Dupanović about 14 years
    Some people love that one tool can satisfy and facilitate all their work, other people become overwhelmed or feel bloated by so many possibilities, much of which they feel they've no interest to. You should really try investing a good week into it and see if it could work back for you.
  • Neil Traft
    Neil Traft over 10 years
    Do you know what is the effect of "you will get a warning, but click proceed anyway?" Is there any reason not to select the system libs? It seems that if I don't select them, it runs fine, but the editor gives errors because it can't find the system libraries. (I guess you don't usually edit your files in Eclipse, only debug?)
  • A. K.
    A. K. almost 10 years
    Excellent answer. Thanks,