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

Tuesday, September 26, 2006

Pydev 1.2.4 released

This release fixes a number of minor things from the last release and adds some new features... I'm not sure if it was the alignment on the planets, but after only 5 days a considerable amount of stuff was done, so I think that a release was a good option (even without any outstanding bugs in the previous release)

Some noteworthy things:

- A long-open debugger bug was fixed with a patch from Kovan (on linux, sometimes it would not show variables after a step-over)

- Yield is correctly treated as an expression now for python 2.5

- Code-completion for parameters: in the open-source version it will get the completions for the tokens that are previously defined in a context and offer them as completions. In the extensions it will bring suggestions with all the tokens available for class methods/attributes for the project. Currently it shows all, but I intend to filter those later, to base it on previous tokens already declared in the context.

- Code-analysis: now won't give you errors if it finds:

  • classes that have __getattribute__ or __getattr__ overriden

  • assigns that are initially None

  • assigns to a function call and not a class definition


Hope you enjoy!

Grab the extensions at:http://www.fabioz.com/pydev/

And the open-source at:http://pydev.sourceforge.net/

Tuesday, September 19, 2006

Pydev 1.2.3 released

Nice numbering hum?

It's out -- support for 2.5 right on time with the python 2.5 release... I didn't really plan for it like this, but I think it's a nice touch ;-)

In case you want the details for the release, check:

Pydev Extensions: http://www.fabioz.com/pydev
Pydev: http://pydev.sf.net

And I've covered most details on my previous posts, so, I'll leave this one short.

Sunday, September 17, 2006

Support for python 2.5 finished!

Ok, the support for python 2.5 should be finished (surely there will be some things I'm missing, but it should be already ready for use... still, I'd currently regard it as unstable, as there were many changes in the core to make it work)

There are some things that will not go a 100% on the way python does things. Namely:

1. 'with' will always be highlighted as if they was a keyword, so, if you have code such as 'with = 1' it will be highlighted as a keyword even if you're using the grammar for python 2.4

2. If you say you're using python 2.5 in your project, it will give errors when:
- it finds the 'with' used in its old form (python would only warn you about it)
- it finds the new construct used without the 'from __future__ import with_statement' (that's the same behaviour python has)

3. Using the from __future__ import absolute_import will currently have no effect for pydev (so, it will still go after relative imports even if you specify it (unlike the ones specified before, I hope to change that in a later release).

There are some specifics to the pydev working also:
- It will now use for external files the same grammar that is specified for the actual interpreter, so, if your interpreter is for version 2.5, that's the version that it will use to do it (now and then I receive a message from people with custom interpreters, so, it is important that those interpreters have the sys.version_info return the correct info when setting up the interpreter)

- If you've specified the interpreter in a previous version, it will acknowledge it as version 2.4 even if it is actually version 2.5 (so, you have to remove and add your interpreter again).

I hope you enjoy it... it should be out in the beggining of the week... hopefully on monday or tuesday.

Tuesday, September 12, 2006

A little bit more from the parser

Ok, I think that the parser is working for all the changes in the 2.5 grammar

Adding the unified try..except..finally statement was only a change in the grammar, and it produces the same AST as if it was a try..finally with a try..except, so no semantic changes should be needed.

The new "with x: ..." statement was also added, but I still have not checked which semantic changes are needed. It will also need a new keyword in the syntax highlighting.

So, the parser is now almost complete... almost because now we have to mark those new constructs as invalid if the grammar is in 2.5 but you're using code in 2.4. Still, that should not be difficult to do.

Thursday, September 07, 2006

Support for Python 2.5

No, it's not ready yet, but it has progressed a lot today.

Pydev now already has parser support for some of its new features that require changes in the grammar:
- The new relative import (from .. import foo / from ...bar import foo)
- The new if expression (a = 1 if bla else 2)

The code analysis seems to be working fine for the new if expression, but the new import will need additional work for recognizing it semantically (so, code completion still does not work with that new version).

Adding that new if expression was really tricky... Also, I always have to stare to the grammar and the javacc file for about 1 hour until it makes sense again ;-)

And here are some links needed to make those upgrades...
-- python grammar: http://svn.python.org/view/python/trunk/Grammar/
-- python parser (contains a version of Python.asdl for python: http://svn.python.org/view/python/trunk/Parser/)
-- what's new in python 2.5: http://docs.python.org/dev/whatsnew/whatsnew25.html

Also, it's a holliday here in Brazil, so, I won't be online again until monday...