Wednesday, January 16, 2013

Django, PyDev, Virtualenv Setup

[UPDATED 2013-01-16]

Django integration in PyDev for eclipse is sweet, and contrary to what my previous post here said, it is not tricky. Although there must have been some reason I made this post right? The only gotcha for me, is that in addition to selecting your virtualenv as the Python interpreter, you must also select the standard Python lib folder so that threading.py and a few other packages that PyDev uses will be available. Luckily, PyDev gives you a nice warning message when you select a virtualenv that says exactly that.

Now selecting this folder should be straightforward but no real guidance is given because each system is a little different. For example on Ubuntu and system installed Python you would add the /usr/lib/python/ while on Windows using the official Python Windows installer, you would use C:\Python27\lib\. And of course you could use any local version of Python that you built yourself, where ever that might be. The easiest way to locate the correct folder is to find the standard Python libraries, threading.py in particular since PyDev specifically mentions it.

Now originally I also had posted an issue with django-admin.py being confused between the system installed version and the version in a virtualenv, but I have not seen that issue with the latest version of PyDev, and I am also using the global site packages in the virtualenv, which also has a different version of Django than the base install. So ... ?

To sum up, assuming you already have eclipse with PyDev installed:

  1. Install virtualenvwrapper: pip install virtualenvwrapper. This will also install the latest version of virtualenv.
  2. Make your virtualenv: mkvirtualenv myDjangoProject. This will also switch on your virtualenv.
  3. Install django in your virtuenv: pip install django. Also install anything else you want, and/or use toggleglobalsitepackages to turn on/off sitepackages.
  4. In eclipse under the Window menu option select preferences and then from the list of preference settings on the right, select PyDev > Interpreter - Python. Now in the Python Interpreters window, click New, type in a name, then browse to ~/.virtualenv/myDjangoProject/bin/python or %USERPROFILE%\.virtualenvs\be\Scripts\python.exe on Windows, click the open button then OK.
  5. Generally I don't alter or add any folders or built-ins at this next step; I just accept what PyDev tells me I need. However, as discussed above, when using a virtualenv, the standard python library folder must also be added, so do it now. Check the box for either /usr/lib/python/ or C:\Python27\lib depending on your system. If successful, you should be returned to the Python Interpreters window, and you should see your new interpreter listed with the default one. Voila.
Voila! Now you should be able to use the Django integrated submenu options with your PyDev Django projects. You can start an app, run any custom manage.py command, start a Django shell and sync your database. I hope it works for you. Enjoy and happy coding!
Fork me on GitHub