Using Eclipse and PyDev for django

This guide assumes you’ve already installed Eclipse, PyDev, Python and Django. It also assumes you’re using Eclipse 3.2, PyDev 1.2.4, Django 0.95 and Python 2.4.

  • Go to Window->Preferences->Preferences->PyDev->Python Interpretter and add the django source file to the PYTHONPATH settings.
  • Create a new PyDev pyhon project. Make sure you uncheck the ‘create src folder’ option.
  • Create project on the command line using django e.g. django-admin.py startproject mysite
  • In your newly created project directory create a src directory in it, and move the django generated source files here
  • In eclipse, right-click your project and select refresh
  • Right-click on the project and select Properties->PyDev – PYTHONPATH, and add your src folder to the project source settings

That should be it. I still get red underlines on the Django source imports even thought PyDev seems to know about them – to test this is working properly, open up your urls.py file and ctrl click on the patterns call – it should take you to defaults.py.

Now you can go ahead and create your database & super user.

Launching to built-in server

Open up manage.py and hit F9. This should print out the usage information for the server. To actually start the server, select Run->Run…, and in the Arguments tab for manage.py enter runserver—noreload. The noreload argument gives you output.

Thanks go to PyDev creator Fabio Zadrozny for his guide which got me going.

This entry was posted on Monday, October 9th, 2006 at 12:17 pm and is filed under Django, howto. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

9 Responses to “Using Eclipse and PyDev for django”

Hi,

2 questions :

1. django-admin.py startproject mysite is typed into eclipse console or in a term ?

2. Why do you need to create a src directory ? I think you do not need it, it seems to work without moving everything to src directory.

Nicolas

django-admin.py startproject mysite is typed into a console, and I created a src directory as the PyDev documentation seemed pretty certain it needed it.

I’m still trying to fix the ‘unresolved import’ issues – I’ll post an update when done.

Indeed, we need a “source directory” but it can be set at project level :

See : http://pydev.blogspot.com/2006/09/configuring-pydev-to-work-with-django.html#116048251566601515

I’ll see how does it work soon…

Once the webserver is running (manage.py—noreload) how can the server be stopped?

Once the webserver is running (manage.py—noreload) how can the server be stopped?

That’s the problem I was having. After the first run, it kept telling me that port 8000 was already taken. I ended up having to drop down to the shell and manually kill the process.

I tried this last night. PyDev redlining all the django imports made me think that the whole thing was hopeless. Good to hear that it still can work.

I’d love it if I could consolidate Eclipse, PyDev, Django, Aptana, and YUI into one development environment. As it stands, I’m spread out over Komodo, Eclipse/Aptana, Dreamweaver, shell, and a number of specialized text editors. Makes it hard to think straight :-)

BTW, in theory, it should be possible to use Aptana’s javascript actions to run boilerplate django command-line stuff (like project creation, db synching, etc.) since their actions can shell out. I haven’t tried it, but all the bits look to be there.

A couple more things:

– You don’t need to move your sources into ‘src,’ but you may have to create a dummy ‘src’ directory just to keep PyDev happy. Actually moving your files into the ‘src’ subdirectory will screw up the URL defaults, so you’ll have to tweak urls.py and settings.py if you really want to do it. It’s a big pain. Might as well leave them in the top-level directory.
– Set up to run manage.py as described above, but to kill the runserver session, you HAVE TO hit the red ‘terminate’ square on the console toolbar BEFORE you run again. If you forget to terminate before running (or debugging) again, you’ll end up with the dread ‘address in use’ problem and you’ll have to manually hunt down the process and kill it. After the third or fourth time you forget, it’ll become a dog-bell-drooling sort of thing. Meanwhile, you might want to make sure you do your development out of the earshot of impressionable children.
– Debugging the source works great! I’ve always been a fan of the Eclipse debugger. I especially like how much context information the Eclipse debugger provides when you inspect various django-related objects. Don’t forget to terminate the session, otherwise the port stays locked up.
– If you’re using the development branch of Django off the subversion repository, the instructions say that you should not install django using the ‘setup.py install’ command, but instead to create a symbolic link in the python ‘site-packages’ directory. That way, you just do an SVN update and you’re good to go. PyDev/Eclipse, however, likes to scan the python path tree and build a reference cache. So if you’re using the Django development branch, remember to look in the Pydev Python interpreter preferences (as described above) , remove the item, then add it in again so the cache gets rebuilt.
– Somewhere along the line, the redlined Django import problems seem to have gone away. Not sure why. Not that I’m complaining or anything …
– Given the above hassles, I’m not sure I’m ready to switch over Django development to PyDev yet. The debugger is a lot better than Komodo, but the rest of the workflow is a pain. Like I said in a previous post, the integration with AJAX debugging may be enough to make one overlook the pain points. YMMV.

About the unresolved imports…

Lets say you import your models like that:
from my_project.my_app.models import Model

And your system path for the project is:
c:\my_web_projects\my_project\

Inside Eclipse :
Window menu > Preferences…
Pydev > Interpreter – Python
System PYTHONPATH > New Folder
Folder : c:\my_web_projects\

And that should do the trick !

People often use to make the mistake of adding c:\my_web_projects\my_project\

Nice one – thanks for the tip Samuel

Thank you, it’s very helpful!

Have your say





Fields in bold are required. Email addresses are never published or distributed.

Some HTML code is allowed:

URIs must be fully qualified (eg: http://www.domainname.com) and all tags must be properly closed.

Line breaks and paragraphs are automatically converted.

Please keep comments relevant. Off-topic, offensive or inappropriate comments may be edited or removed.