magpiebrain

Sam Newman's site, a Consultant at ThoughtWorks

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.

About these ads

16 Responses to “Using Eclipse and PyDev for django”

  1. NiCoS

    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

    Reply
  2. sam newman

    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.

    Reply
  3. Ed

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

    Reply
  4. 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.

    Reply
  5. 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.

    Reply
  6. Samuel Adam

    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_projectsmy_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_projectsmy_project

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

    Reply
  8. 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

    Reply
  9. adam mitchell

    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

    Reply
  10. compro oro roma

    An individual completed various good things now there. I did a search for the subject matter and found the majority of folks will certainly consent with your weblog

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Basic HTML is allowed. Your email address will not be published.

Subscribe to this comment feed via RSS

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: