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.

3 Trackbacks

You can leave a trackback using this URL: http://www.magpiebrain.com/2006/10/09/using-eclipse-and-pydev-for-django/trackback/

  1. [...] Using Eclipse And PyDev For Django [...]

  2. By Eclipse PyDev开发Django | 52.bingcheng on October 8, 2009 at 12:12 pm

    [...] entry through the RSS 2.0. You can leave a response or trackback to this entry 原文:http://magpiebrain.com/blog/2006/10/09/using-eclipse-and-pydev-for-django/ 最近学习Python语言,采用的Python Web 框架是 [...]

  3. [...] Using Eclipse and PyDev for django – Magpiebrain [...]

12 Comments

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

    Posted October 9, 2006 at 9:22 pm | Permalink
  2. 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.

    Posted October 10, 2006 at 10:03 am | Permalink
  3. 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…

    Posted October 10, 2006 at 8:31 pm | Permalink
  4. Ed

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

    Posted December 12, 2006 at 9:49 pm | Permalink
  5. Ramin

    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.

    Posted December 15, 2006 at 9:28 pm | Permalink
  6. Ramin

    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.

    Posted December 15, 2006 at 10:17 pm | Permalink
  7. 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\

    Posted April 6, 2007 at 5:45 pm | Permalink
  8. sam

    Nice one – thanks for the tip Samuel

    Posted April 9, 2007 at 12:50 pm | Permalink
  9. rodin

    Thank you, it’s very helpful!

    Posted October 12, 2007 at 7:28 am | Permalink
  10. me

    I have found that on Windows launching a python script/process from within Eclipse and then using the Stop button does not actually kill the python process (e.g. manage.py runserver). This can be *VERY* confusing if you are developing and think you are running something that you are actually not. I have to kill the python.exe instance in task manager.

    Posted November 27, 2008 at 11:35 pm | Permalink
  11. new_in_django

    @Samuel Adam:
    Probably the better way is to:
    In PyDev? Package Explorer Window Right click on project name, choose Properties
    In External Libraries tab, choose “Add source folder” and select the folder containing your project dir (e.g. /home/user/django if project is /home/user/django/mysite)
    as this decouples general pydev settings from project settings

    Posted January 2, 2010 at 12:10 am | Permalink
  12. if your python process are not ending make sure you have –noreload in the debug args. the red-button killed the processes with this arg but not without it

    Posted May 6, 2010 at 3:50 am | Permalink

Post a Comment

Your email is never shared. Required fields are marked *

*
*