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
Now you can go ahead and create your database & super user.
Launching to built-in server
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
runserver --noreload. The
noreload argument gives you output.
Thanks go to PyDev creator Fabio Zadrozny for his guide which got me going.
16 Responses to “Using Eclipse and PyDev for django”
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.
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 🙂
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:
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
Nice one – thanks for the tip Samuel
Thank you, it’s very helpful!
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.
[…] Using Eclipse And PyDev For Django […]
[…] 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 框架是 […]
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
[…] Using Eclipse and PyDev for django – Magpiebrain […]
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
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