Sunday, March 21, 2010

Eclipse + Pydev + Django

So I decided that I wanted to learn Python. On top of that I wanted to learn Django. The catch is that I like to do everything from within Eclipse so that I can make use of the debugger. I couldn't find information about how to do all of this so instead I pieced it together here. My overall goal is to run through this Django Tutorial but this post will only deal with setting up Eclipse/Pydev and Django.

NOTE: Hopefully soon this post will be rendered useless when Pydev includes Django support.

Install Pydev

Since I use Eclipse for just about everything from Java to Ruby on Rails to ActionScript I obviously already had it installed so the first thing I needed to do was to install the Pydev Eclipse plugin. Pydev is installed just like any other plugin and instructions on how to install Pydev can be found here.

Install Django

Download Django from here then unzip it and navigate to the unzipped folder and run the following command.
python install

I am on a Mac with Python 2.6 installed so Django is installed at

Create a Django Project

Now I should be ready to create my project and the configure it to work with Eclipse/Pydev. First I need to navigate to my folder where I want to create the project and type the following. startproject mysite

Now this results in the following
cd mysite/

As you can see all the project files are created at the root. Generally projects consist of more than just python files. Also, pydev likes the code to be in a "src" folder so lets move the python files to a "mysite" package in the "src" folder.
mkdir src
cd src
mkdir mysite
mv *.py src/mysite/.

Create the Pydev Project

Now I can launch Eclipse and create a new project by going to File-> New -> Pydev Project. For the directory I will select the project folder I created earlier.

Click "Finish" and now I have a Pydev project that looks like the following.

Add Django to the Project

The first thing I need to do now that I have the project is tell it where to find Django. I can do this by going to Project -> Properties. Here I need to add the installed location of Django as an External Library.

Starting My Server From Eclipse

In Eclipse I go to Run -> Run Configurations... and create a new Python Run configuration. Select my project and for the main module provide the path to the file.

Switch to the arguments tab and add the "runserver --noreload" argument.

Click "Apply" and now click "Run" and navigate to http://localhost:8000.

This is as far as I have gotten so far. Next I will actually try to code something.