Friday, September 29, 2006

Configuring pydev to work with django

Update: Pydev now has a new Django integration. See: http://pydev.org/manual_adv_django.html



Ok, some time ago I've covered how to configure pydev to work with Turbogears, and since them, I've been having some requests to do the same for Django, so... here we go ;-)

As with Turbogears, I still don't have any previous experience working with Django, so, it may be good to see how easy it is to get started with it for others!

First step: get Django. Obvious place (after searching for it): http://www.djangoproject.com/

I started by reading the overview, and it seems to be straightforward... now on to something real: downloading it... it says it only needs python, but I didn't find information on which is the preferred version. I googled for it and it appears that it is still not working well with python 2.5, so, I'll go with python 2.4.

Second step: install

[C:\bin\Python24]python e:\Install\Dev.Python\Django-0.95\setup.py install
>> The required version of setuptools (>=0.6c1) is not available, and can't be installed while this script is running. Please install a more recent version first.


The tutorial says that it will attempt to grab setuptools if setuptools is not there, but unfortunatelly it seems it does not upgrade if a version is already there.

So, after googling a little, I found http://peak.telecommunity.com/DevCenter/EasyInstall#installing-easy-install and discovered that I had to download an ez_setup.py script and execute it. It appears to work without any problems, so, on to trying to install django again...

[C:\bin\Python24]python e:\Install\Dev.Python\Django-0.95\setup.py install
still gives me problems, because it requires me to be in the same folder where setup.py is...

[E:\Install\Dev.Python\Django-0.95]c:\bin\Python24\python.exe setup.py install
seems to solve it... smoke test: python -c "import django" seems to work ok

It also requires a database (as I'm just testing, pysqlite seems a good option -- I'm using version 2.3.0)

Now, on to re-configure the pydev interpreter to add those new paths... I'm kind of lazy, so, usually I just remove the interpreter and add it again, instead of figuring out just the new paths (I also thinks it's safer, as it gets all the folders it finds in the pythonpath automatically). More info on that at http://www.fabioz.com/pydev/manual_101_interpreter.html

Now, on to the actually doing something

To create a base project it provides a script, so let's do it:

django-admin.py startproject mysite

It created a 'mysite' folder with some files... still in the command line (inside the mysite folder), it says to start django with:

python manage.py runserver

And voila... it's running. Now, on to do that from inside Eclipse.

Eclipse requires your code to be under a project, so, the 'new pydev project' wizard should be used, pointing the project contents to the 'mysite' folder or to a parent folder. As there is usually much more than just code in a project -- like folders for python, c++, docs... and as in this example we surely won't have a c++ folder, the initial project structure became:
/mysite
    /docs
    /src <-- this is the folder that should be added to the project pythonpath (in the project properties)
        /mysite <-- I moved the initial files to this folder (so that we can make "import mysite" as the src is added to the pythonpath)

-- for more details on creating a project and setting the project pythonpath, check: http://www.fabioz.com/pydev/manual_101_project_conf.html

Now, on to starting the server from within pydev:
If you just run the manage.py file (just open it and press F9), it will show you a message with its usage, so, run it once and then go to the menu: Run > Run... and select the 'mysite manage.py' run and put in the program arguments runserver.

Now, running it like that leaves us without any prompt inside of the Eclipse console. Taking a closer look in the django code, it appears to have an autoreloader structure (I guess that's something common to web-servers), so, as it is actually run in another process, we won't have any output. To fix this in the testing environment (after killing the process inside of Eclipse and the other process that is still running in the computer), we can just add another argument, so, now as arguments we have runserver --noreload

Note that the Eclipse console currently does not have a Ctrl+Break, so, you have to actually kill it in the red button in the console, and to re-run the last run, just use Ctrl+F11.

Now, on to set the sqlite parameters and create the actual database -- according to the tutorial, just set the engine and file to be used for sqlite and the on to:

python manage.py syncdb

-- I usually do that stuff on a separate prompt, as it is nice to run things inside of Eclipse, but having to change the argument for each different run is not all that nice (so, an open shell is always welcome)

and created a superuser... if you configured the auto-refresh for Eclipse as in http://www.fabioz.com/pydev/manual_101_eclipse.html, a new file should be appearing in the database file for sqlite -- if you pointed it to some file inside of your project (if you still haven't the auto-refresh configure, I really advise doing it, as it can be a pain having to F5 for it to refresh the folders when some external change is done to the filesystem)

Now, keeping on with the tutorial:

python manage.py startapp polls

and copied the example from the homepage. Defining the model seems straightforward, and code-completion / code-analysis should be already working ;-)

Making the syncdb again updates my sql with the new structure (pretty nice).

Now, adding the admin interface seems pretty straightforward, and without the auto-reload, the regular debugger works pretty well -- test it by putting a breakpoint after creating the __str__ function for the Poll class in the example and pressing F11 (used to debug the last run). If you want to keep the auto-reloader and still debug, you'll have to use the remote debugger that comes with the extensions to debug, as it does not rely on having the process started from Eclipse. Check http://www.fabioz.com/pydev/manual_adv_remote_debugger.html for more details.

And I guess that's it for the pydev configuration... code-completion, code-analysis, debugger, etc. should all be working.

You can now enjoy working with django inside of pydev!

Cheers,

Fabio

28 comments:

Anonymous said...

Thank you so much for pydev.

Kevin Menard said...

Just an FYI, when running in the debugger, I had to set the base directory to the one containing settings.py, otherwise runserver wouldn't run. I did not have to do this when running without the debugger. So, I'm guessing the debugger starts in a different base directory.

Just figured I'd throw it out there to save other people some time.

Anonymous said...

One question : should or must we use a src folder ?

Would it work if we import data at project level instead of in src subdirectory ?

Fabio Zadrozny said...

You must have a source folder (but you can set the source folder to be in the project level, and not actually the 'src' folder) -- so, as long as you have your project pythonpath correctly set, you can have the structure that suits you better.

Cheers,

Fabio

Anonymous said...

Hi again,

Thanks for your answer.

To be sure I understand everything :

What I made so far :

- First, in eclipse, I set python interpretter and add django folder into python path.

- I opened SubEclipse and chose to checkout a projet and use Pydev projet wizard.

- At this stage, I have autocompletion and so on. Does it mean that pydev project folder is considered as a "source folder" by itself ?

Otherwise, I do not see in eclipse how can I check out my project into the "source folder" from a pydev project... any idea about this ?

Fabio Zadrozny said...

Check http://www.fabioz.com/pydev/manual_101_project_conf2.html to see how to configure that.

Cheers,

Fabio

Anonymous said...

Thanks for this note. The one thing I can't get to seem to work is code-completion when it comes to the Django code. I've added Django to the PYTHONPATH and it looks like there's some sort of scanning going on, but when I try to add any code that references Django, for example in the polls/models.py, while adding:

votes = models.IntegerField()

I get no code assist, but if I reference something in the standard library, like:

return self.pub_date.date()

I get full code completion. Any tips would be appreciated.

Fabio Zadrozny said...

Please, report that as a bug-report in the sourceforge tracker (check the faq: http://pydev.sf.net/faq.html if you have doubts on how to do that).

Cheers,

Fabio

p.s. Have you tried removing and re-adding your interpreter (or adding django to the forced builtins)?

Anonymous said...

I'm a huge django fan and nube, and now a huge pydev fan.

Thank you so much for this product.

Y

Anonymous said...

Sorry, but what is this? Thoughts of a Programmer, written down in 30 Seconds? I could do that. Plz Make a clear, concize beginner-readable Text out of it or rename it to "Thoughts, flying through my Head while i configured pydev with Django"

some questions (i use win XP):
Where does pydev store the pythonpath-Data? I have Paths there , which are not in my pythonpath, stored in the registry. Pythonpath, under ...
HKEY_LOCAL_MACHINE/SOFTWARE/Python/
PythonCore/2.5/PythonPath
... contains:
M:\bin\python25\Lib;
M:\bin\python25\DLLs;
M:\bin\python25\Lib\lib-tk

in Pydev (preferences/Interpreter - Python) there are more. Does Pydev search recursive the system Pythonpath ? (which, as far as i know, python itself doesnt)

i also have a Python.zip in my system32-Folder in the pydev listing of paths (that zip doesnt exist). What is it?

Whats the difference between "system libs" and "libs found in the pythonpath (dlls)"?

And, there are not just dlls, there are also pyds listed. Did u mean "libs found in the pythonpath (dlls and pyd)" ? Then why dont u write it?

So, this were the Questions i got from a few german Kids, while i tried to explain them the Pydev Configuration, together with django.

I couldnt answer the questions, can u ?

(and last, not least, pydev is a very nice tool and i enjoy working with it - thanks for it)



Greetings, Nils

Fabio Zadrozny said...

Hi Nils...

Well, yes... this is not intended to be a very detailed thing (that's why it's in this blog and not in the actual homepage)... But if you do have something more formal I'll gladly publish it in the appropriate space.

Now, on to your questions...

1. The pythonpath you see is the same that python uses... pydev spawns a shell and does:
import sys
for p in sys.path: print p

The one in your registry is what python uses as the basis for that 'real' one (there are more ways to add paths to your pythonpath than through the registry, such as .PTH files or simply configuring your PYTHONPATH env variable).

Note that you can edit the pydev pythonpath to suit your needs (so you can add or remove libraries you want to use or not).

I don't know why python puts that .zip there (pydev just gets that info from the interpreter... you can just remove it without any problems)

2. Pyds are basically dlls (if you just rename a .dll to .pyd python will recognize it the same way), but that's different from platform to platform (e.g.: in linux it can be .so)... dll is to mean dynamically linked libraries (which can have any of those extensions).

3. System libs are the folders added to your pythonpath. The dlls is just more information for you to see (what appears there doesn't actually make any difference).

Cheers,

Fabio

Anonymous said...

Hi again,

thanks for detailed answer, didnt know that dll-thing :-)

Im planning a videotutorial, showing how to install a eclipse-pydev-django-cvs-apache-
modpython-etc-environment on win. When im ready I post a link to it here.

I found a posting describing another few ways to set the Pythonpath, its in german, but perhaps helpful to some...

Greetings, Nils

Anonymous said...

Hi everybody!

I have a good way to debug django with autoreload feature. please see my blog article:



Thanks.

Anonymous said...

The url is:
http://bear330.wordpress.com/2007/10/30/how-to-debug-django-web-application-with-autoreload

Anonymous said...

Great work, Fabio. Using your guide I can run the server from within Eclipse, and even use autocomplete. Unfortunately, my breakpoints and watches are completely ignored. It works correctly in WingIDE but I'd much prefer to use Pydev/Eclipse. Any ideas on what could be causing it? I followed your guide to the letter (several times).

Thanks again!

Fabio Zadrozny said...

Can you report that as a bug in the sf tracker? (with details on your env. and where are you placing the breakpoints).

Cheers,

Fabio

Josir Gomes said...

Great work Fabio!!!
It works fine with Ubuntu 8.04 and Eclipse 3.2. Debug works too.

Do you intend to write "snippets" to automate syncdb, shell? Sorry I am very lazy to enter on prompt :))

Thank you!

PS: Se eu ganhar um dinheiro com isso, com certeza farei uma contribuiĆ§Ć£o para o projeto!

Cherian A.K.A Ryan said...

hy hi fabio,
when i try to run manage.py withe the argument runserver --noreload. i get the folowing error.
'--noreload' is not a valid port number
What could be the reason.
Regards,
Cherian a.k.a Ryan

Fabio Zadrozny said...

Hi Josir,

I'm not really sure... until now Pydev has no special support for any specific project, but it could be an interesting addition (maybe on an optional plugin).

Please report that as a feature request.

Fabio Zadrozny said...

Hi Cherian,

I think that the guys at the Django forum can explain that better...

Anonymous said...

I'm new to python and interested in working with the Google AppEngine. An article or pointers on how to get pydev to write app engine apps would be very nice and appreciated.

Fabio Zadrozny said...

I was going to do it, but there's already one interesting tutorial on that at: http://code.google.com/appengine/articles/eclipse.html

Unknown said...

Eclipse keeps crashing when stopping the server with the red button. I'm on a mac. Do you know why?

Fabio Zadrozny said...

If it's crashing it's a vm bug... Try changing the java vm you use.

p.s.: this blog is not the appropriate place for these doubts. Please ask at http://sourceforge.net/forum/forum.php?forum_id=293649 or report bugs following: http://pydev.sourceforge.net/faq.html#how_do_i_report_a_bug

Anonymous said...

For Debian or Ubuntu users, for which only an old and disfunctional version of pydev currently is available, simply install the available/prefered Eclipse, Python, Django and Pydev versions to meet all dependencies (you may have to use unstable repositiories if you cannot find Pydev in testing or stable).
Then get the latest Pydev release (a zip containing org.python.pydev.*, com.python.pydev.* folders) from pydev.org. Copy that stuff to the appropriate folders, where also the old, above installed version is located (/usr/lib/eclipse/features and /usr/lib/eclipse/plugins). It should be obvious what belongs where.
Restart Eclipse and the World should be perfect again.

:-)

Raja said...

To make Django Debugging with Autoreload, there was a hack posted at
http://bear330.wordpress.com/2007/10/30/how-to-debug-django-web-application-with-autoreload/

Unfortunately, this dint work as is with Django 1.1. The updated hack can be found at

Debug Django 1.1 with autoreload

Anonymous said...

When launching the "manage.py syncdb" on gets prompted whether you want to create a superuser. When entering the password, what you type is visible as clear text.

Fabio Zadrozny said...

Unfortunately, eclipse does not have the needed support for launching a realy tty terminal in the console view. See: https://bugs.eclipse.org/bugs/show_bug.cgi?id=122429

Unfortunately, until that's solved, there's not much pydev can do about it.