Sunday, December 6, 2009

Eclipse Pydev for Django with autoreload

In an attempt to use Eclipse for Django, I was looking around to find how I can maximize productivity. IDE provides a few good things like auto completion and debug but if you were to work around it even with an IDE, then its probably not worth it.

Found "How to debug django webapp with autoreload" . That was a smart hack to hook into the autoreload mechanism to set up pydevd.settrace. Its a real pain without that to put in a

    import pydevd

in all places that you want to debug. With an IDE, you should be able to setup a breakpoint, hit the code , let it stop where you set the breakpoint and move on.

The post above indicated how things probably worked for Django pre 1.0 and dint work for me with the 1.1 codebase, Here is the modified code for that worked with Django 1.1

command = None
if len(sys.argv) != 1:
    command = sys.argv[1]
if settings.DEBUG and (command == 'runserver' or command == 'testserver'):
        import pydevd
        inEclipse = True
    except ImportError:
        inEclipse = False

    if inEclipse:
        from django.utils import autoreload
        m = autoreload.main
        def main(main_func):
            import os
            if os.environ.get("RUN_MAIN") == 'true':
                def pydevdDecorator(func):
                    def wrap(*args, **kws):
                        pydevd.settrace(suspend = False)
                        return func()
                    return wrap
                main_func = pydevdDecorator(main_func)
            return m(main_func)
        autoreload.main = main

Placing the above code in after importing the "settings" module seems to allow debugging with autoreload option on.

I also added some logic to check if Im running in Eclipse or not. I dont use Eclipse when Im not using my laptop -- vim is my friend. Its not a robust check, but works to check if we are in Eclipse or not. The idea is to not go into the auto-debug block if you are not running in Eclipse (for e.g. when you are running with vim and a command line).


  1. I can't get it working. The debuger does not stop at my breakpoints if they are ouside of the I tired to debug a view call.

    Can you give me any further advice??


  2. I observed the same as Sebastian...

  3. Got it to run:

    The problem was that dydevd was'nt found (and the "inEclipse-Test" hides that).
    You will find this module in the eclipse/plugin dir.
    I just did it ugly and added it via "project properties/external source dir".
    Then don't forget to start the Debug Server AND(!) the debug session as well.

  4. Thx for the hint Pit. Now it works!
    I think you know that, but you also can add org.python.pydev.debug_...\pysrc to your Python libs.

    Everytime I start the django app it detaches from eclipse, so i can't terminate it. Any idea how to terminate a django from inside eclise?

  5. works also in aptana

  6. Did anyone find answer to Sebastians question?

  7. I had to add "import sys" to the very top too.

    --Trindaz on Fedang #django

  8. For Django 1.3 you need to have:

    def main(main_func, args=None, kwargs=None):

    instead of just:

    def main(main_func):