yet another blog.

October 30, 2007

How to debug django web application with autoreload.

Filed under: Uncategorized — bear330 @ 8:02 am

Edit:

If you are a django newbie and want to develop django application in pydev, see this article first: Configuring pydev to work with django.

This post will focus on how to debug django without –noreload option.

Configuring pydev to work with django this article mentioned that:

“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.”

I tried it and feel it is a very bad way to debug django application. I use auto reload feature because its dynamic and convenient. But if follow the article to do that, I must insert a line: “import pydevd; pydevd.settrace()” in my source code to set a breakpoint. How silly of this way? If I want to trace the source code in django itself, I must modify the source code of django just for a breakpoint. What if I forget to remove this line…?

Ok, you can give it up that, debug django with –noreload option, not really bad, right? This is true if your web application do not need to login. Every time my partner debug its code, he must change the code, restart django server and login again, then to see the result and do real debug. Restart and login again is a redundant operation in debugging and very wasting time (I know I can use some automation tool such as Selenium to reduce my pain, but it is also inconvenient). The django’s charming productivity is gone.

After some digging into source code of django and pydev, I found a easy way to debug django in pydev with autoreload feature. This way is also using pydev extension’s remote debugger, but the annoy operation which inserts a “import pydevd; pydevd.settrace()” is no more needed. You can set a breakpoint like before and debug it with autoreload feature. The django’s productivity is back!

How to do that? This is simple, you must modify your manage.py and insert these code:
(This code requires pydevd module, please see Configuring pydev to work with django to know where to find it.)

    if settings.DEBUG and (command == "runserver" or command == "testserver"):

        # Make pydev debugger works for auto reload.
        try:
            import pydevd
        except ImportError:
            sys.stderr.write("Error: " +
                "You must add org.python.pydev.debug.pysrc to your PYTHONPATH.")
            sys.exit(1)

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

            return m(main_func, args, kwargs)

        autoreload.main = main

This code overrides the django’s autoreload behavior to make pydev can communicate with it. The knowledge behind it is a little complex, so I don’t explain it in here.

If you want to debug without –noreload, you must start remote debugger first, then run django server in pydev (no matter debug mode or not). Eclipse will suspend while it run on your breakpoints. You can modify code and do not need to restart django server to debug it. Try it!

47 Comments »

  1. Thanks a lot of this mini guide. It was very helpful. Everything works as expected, but I keep getting this warning when doing a normal “manage.py runserver” in eclipse while the pydev remote debug server is running:

    —————————-
    PYDEV DEBUGGER WARNING:
    sys.settrace() should not be used when the debugger is being used.
    This may cause the debugger to stop working correctly.
    If this is needed, please check:
    http://pydev.blogspot.com/2007/06/why-cant-pydev-debugger-work-with.html
    to see how to restore the debug tracing back correctly.
    Call Location:
    File “/Applications/eclipse/plugins/org.python.pydev.debug_1.3.10/pysrc/pydevd.py”, line 743, in settrace
    sys.settrace(debugger.trace_dispatch)
    ———————–

    Despite the warning, things are working very well so it’s not really a big deal.

    Thanks again!

    Comment by Duc Nguyen — November 26, 2007 @ 12:53 am | Reply

  2. You can run manage.py with “run as python run” option, not “debug as python run”. This warning won’t appear anymore.

    The “debug as …” is only needed when you want to set breakpoint in manage.py or the code executed before auto reload thread.

    In my original post, everything is ok in eclipse, but command line. I do some modification for this to make manage.py runserver work well in command line mode.

    def inReloadThread():
    “””
    Manage.py is called in reload thread or not?
    @return True if we are in reload thread.
    “””
    return os.environ.get(“RUN_MAIN”) == “true”

    if settings.DEBUG and (command == “runserver” or command == “testserver”):

    # Make pydev debugger works for auto reload.
    usePydevd = True
    try:
    import pydevd
    except ImportError:
    if not inReloadThread():
    print (“PYDEVD DEBUG DISABLED: ”
    “You must add org.python.pydev.debug.pysrc ”
    “to your PYTHONPATH for debugging in Eclipse.\n\n”)
    usePydevd = False

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

    return m(main_func, args, kwargs)

    autoreload.main = main

    Please enjoy this.
    I am very happy this code is helpful for you.

    Comment by bear330 — December 1, 2007 @ 5:55 am | Reply

  3. Hi, I was unable to get this to work. Where did you place the code in manage.py?

    In the first line, “if settings.DEBUG and (command == “runserver” or command == “testserver”):”, there is no “command” in manage.py.

    I got rid of this (since I was only running this from Eclipse). After that, it would not connect to the debugger because the port is randomized each time, but the code is using a hardcoded value. I rememdied this with the following hack:

    import os
    import re
    # HACK: Get remote debugger port from ppid
    fd = os.popen(‘ps wwo command -p %d’ % os.getppid())
    re_port = re.compile(r’–port\s*(\d+)\s*’)
    port = int(re_port.search(fd.readlines()[1]).groups()[0])

    I finally got it running with this, but now I get the same warning as comment #1 and none of my breakpoints work. The debugger works fine if I use –noreload with the default manage.py, but I would really like to get this working with autoreload.

    Comment by impulse — December 8, 2007 @ 10:24 am | Reply

  4. Ignore my previous comment. I wasn’t using the PyDev extensions remote debugger. It’s working now.

    Comment by impulse — December 8, 2007 @ 11:29 am | Reply

  5. works like a charm 🙂
    Thanks a lot!

    Comment by Duc Nguyen — December 13, 2007 @ 7:38 am | Reply

  6. very interesting, but I don’t agree with you
    Idetrorce

    Comment by Idetrorce — December 16, 2007 @ 3:03 am | Reply

  7. I desperately want to get this working, but I’m having trouble repeating everyone’s success from almost a year ago. First, I have the same confusion as Duc Nguyen: I don’t know where to drop in this code. When I put it at the top level of my manage.py there is no “command” variable defined.

    Second, even when I leave out the conditional and always run this code, I get the error “No module named pydevd” when it tries to execute the “import pydevd” line. I swear I have this properly in my PYTHONPATH. Indeed, from the Python console, I can import pydevd without any problems.

    Any hints for either problem?

    Comment by digi — September 29, 2008 @ 4:24 pm | Reply

  8. I am very sorry to put a vague code here. The command variable is:

    if len(sys.argv) > 1:
    command = sys.argv[1]

    And you must make pydevd available for your project. In my .pydevproject file, I make it as external source folder:

    <pydev_pathproperty name=”org.python.pydev.PROJECT_EXTERNAL_SOURCE_PATH”>
    <path>C:\Program Files\Eclipse 3.4\plugins\org.python.pydev.debug_1.3.20\pysrc</path>
    </pydev_pathproperty>

    You can set up this by Properties->PyDev – PYTHONPATH page or simply put these lines in your .pydevproject file.

    If you still have any problem, feel free to let me know.

    Comment by bear330 — September 30, 2008 @ 11:24 am | Reply

    • Hi bear330,

      I have the following in my .pydevproject file.

      /home/nicta/eclipse/plugins/org.python.pydev.debug_1.6.5.2011020317/pysrc

      however, when I run my script with the following code at the beginning, it still saying I don’t have pysrc in my PYTHONPATH.

      ####################
      REMOTE_DBG = True

      # append pydev remote debugger
      if REMOTE_DBG:
      # Make pydev debugger works for auto reload.
      # Note pydevd module need to be copied in XBMC\system\python\Lib\pysrc
      try:
      import pysrc.pydevd as pydevd
      # stdoutToServer and stderrToServer redirect stdout and stderr to eclipse console
      pydevd.settrace(‘localhost’, stdoutToServer=True, stderrToServer=True)
      except ImportError:
      sys.stderr.write(“Error: ” +
      “You must add org.python.pydev.debug.pysrc to your PYTHONPATH.”)
      sys.exit(1)
      ####################

      My eclipse is
      Eclipse SDK

      Version: 3.6.1
      Build id: M20100909-0800

      any idea?

      Comment by peizhao — February 18, 2011 @ 11:03 pm | Reply

      • sorry, the debug code should change to

        import pydevd

        it works now.

        Comment by peizhao — February 18, 2011 @ 11:15 pm

  9. Belated thanks for the clarification! This definitely helps me understand your solution.

    Comment by Chris DiGiano — October 31, 2008 @ 5:57 pm | Reply

  10. On Ubuntu pydevd source folder is located

    $HOME/.eclipse/org.eclipse.sdk.ide/updates/eclipse/plugins/org.python.pydev.debug_1.3.24/pysrc/

    Comment by MIkko Ohtamaa — December 29, 2008 @ 10:44 am | Reply

  11. […] should be.  I’ve uploaded that to snippet to django snippets.  It was originally posted here in 2007, so I’ve copied it to Django snippets in case that post […]

    Pingback by Django+Eclipse with Code Complete Screencast « vlku.com — June 11, 2009 @ 12:16 am | Reply

  12. Off topic – need help with email settings
    How do I change Gmails SMTP settings?
    Dr Gil Lederman
    Gil Lederman
    Gil Lederman MD

    Comment by Gil Lederman — September 7, 2009 @ 3:19 am | Reply

  13. Thanks for the hack. I had to make a few changes to get this to work with Django 1.1 (since the “inner_run” method now takes no arguments as opposed to the earlier system where it took the *args and **kwargs). I posted the changes at
    http://rajasaur.blogspot.com/2009/12/eclipse-pydev-for-django-with.html

    Comment by Raja — December 6, 2009 @ 5:07 am | Reply

  14. Hi all,
    I have been following along with this. Having updated my manage.py code to reflect “if len(sys.argv) > 1:
    command = sys.argv[1]”, I now get an error related to sitecustomise.py when I run the debug:

    sys.path.extend(paths_removed)
    AttributeError: ‘NoneType’ object has no attribute ‘path’

    coming specifically from line 148. I’m pretty sure that I have pydevd correctly set up and have it attached to my PYTHONPATH okay. I’m running python 2.6. Has anybody any ideas?

    Thanks in advance,
    G (pretty new to all this)

    Comment by Gerry — December 9, 2009 @ 6:08 pm | Reply

  15. Finally, realize that all the various bookmakers have their own rules and policies regarding how they settle bets done through arbitrage sports betting when they have such irregular results. As with the advice on pricing, be sure to check the rules of the individual bookmakers before you place your bets.

    Finally, to insure that you do indeed have the best betting odds that you can get, it is crucial as a bettor to shop the money line and the odds. Any reputable sports book online would offer the best line for your wagers without coercion. However, once you have found the best deal, it is important to verify and scrutinize the odds based on the past performance of the particular teams involved you can make appropriate judgments that can be indicative of the weaknesses and strengths of the respective sporting teams. You should also do research on the sports players, injury lists, and the teams if possible.
    online football betting
    It appears that the United States legal system is closer than ever before to cutting off ties with any type of online gambling within its boundaries. This includes poker, casinos, and sports betting among others. Generally speaking, anything that has to do with transmitting money via the internet as far as gambling is concerned is being cracked down on quite harshly.

    From the above, therefore, you can see that finding a good Selection System and sticking to it is the only way to go if you are truly serious about making money at sports betting.

    Comment by FrancescaRivierra — May 17, 2010 @ 2:47 am | Reply

  16. txing

    Comment by ppheiko — June 4, 2010 @ 10:56 am | Reply

  17. Welcome First time skipped right here in your web site, founde on ASK. Thanks for the short response. Your information was very element and knowledgeable.

    Comment by Daily File — October 2, 2010 @ 4:50 am | Reply

  18. Please move me if this is not the right area for my post. You can call me william. I research about workout Check out my site diet program reviews. I look forward to reading more about this website bear330.wordpress.com

    Comment by SwewSilmhic — January 16, 2011 @ 10:06 pm | Reply

  19. I was just browsing every now and then along with you just read this post. I have to admit that I am inside the hand of luck today otherwise getting this excellent post to learn wouldn’t are actually achievable for me personally, at the least. Really appreciate your content.

    Comment by tramdol {1|2|3|4|5|6}{2|3|4|5|6|7}{a|1|2|3f|g|hR} — April 7, 2011 @ 5:01 pm | Reply

  20. Длительность диеты 9 дней, и за это время можно похудеть на 8–9 кг. Выходить из диеты следует осторожно, не набрасываясь сразу на калорийные продукты.
    Диеты считаются одними из самых эффективных способов избавиться от лишнего веса. Масса информации, которую необходимо знать о диетах для быстрого похудения
    Самая лучшая эффективная диета для похудения это диета, которая подойдет именно Вам и наиболее эффективно поможет сбросить лишний вес.
    оветы как похудеть – диеты, кремлевская диета, правильное питание, таблицы калорийности, диета, аэробика.
    Тема диет, так или иначе, касается практически каждого человека. У кого-то в семье есть худеющая жена, сестра, мать, а это значит, что ей многого «нельзя»,

    Comment by Swettejup — July 15, 2011 @ 10:09 pm | Reply

  21. I am receiving “NameError: name ‘sys’ is not defined”
    Here is my manage.py

    #!/usr/bin/env python
    from django.core.management import execute_manager
    try:
    import settings # Assumed to be in the same directory.
    except ImportError:
    import sys
    sys.stderr.write(“Error: Can’t find the file ‘settings.py’ in the directory containing %r. It appears you’ve customized things.\nYou’ll have to run django-admin.py, passing it your settings module.\n(If the file settings.py does indeed exist, it’s causing an ImportError somehow.)\n” % __file__)
    sys.exit(1)

    if __name__ == “__main__”:

    if len(sys.argv) > 1:
    command = sys.argv[1]

    if settings.DEBUG and (command == “runserver” or command == “testserver”):

    # Make pydev debugger works for auto reload.
    try:
    import pydevd
    except ImportError:
    sys.stderr.write(“Error: ” +
    “You must add org.python.pydev.debug.pysrc to your PYTHONPATH.”)
    sys.exit(1)

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

    return m(main_func, args, kwargs)

    autoreload.main = main

    execute_manager(settings)

    Comment by eros — October 12, 2011 @ 12:58 am | Reply

  22. […]  How to debug django web application with autoreload.: How to debug django web application with autoreload in Eclipse pydev plugin. […]

    Pingback by Django resources | 岭南六少 - 一朵在LAMP架构下挣扎的云 — October 17, 2011 @ 8:27 am | Reply

  23. эффективные диеты…

    […]How to debug django web application with autoreload. « yet another blog.[…]…

    Trackback by эффективные диеты — January 29, 2012 @ 1:29 pm | Reply

  24. What’s Going down i am new to this, I stumbled upon this I’ve
    found It absolutely helpful and it has aided me out loads.
    I’m hoping to give a contribution & help different customers like its helped me. Good job.

    Comment by Cindy — February 2, 2013 @ 10:50 pm | Reply

  25. Hi there, i read your blog from time to time and i own a similar one and i was just
    wondering if you get a lot of spam responses? If so how do you
    stop it, any plugin or anything you can suggest?
    I get so much lately it’s driving me mad so any help is very much appreciated.

    Comment by glass — April 16, 2013 @ 5:45 am | Reply

  26. This is the point where you can try gh supplement called releasers
    instead. The problem is that people cannot really openly
    acquire these supplements. The availability and lower price of processed and canned foods.
    This in turn will encourage your body to function properly.

    Comment by hghreleasersreview.com — May 2, 2013 @ 11:15 pm | Reply

  27. One should never exercise if nursing schools united states it causes pain.

    Comment by online masters nursing programs — May 9, 2013 @ 1:16 am | Reply

  28. Creighton 58 – Alabama 57 Well, we are planning and expect to open Tinley Park in May.
    Operator This concludes accelerated nursing today’s conference call. And just because there’s
    not — there’s more competition doesn’t mean that you should simply choose the school that can accommodate the student’s needs.

    Comment by what are the best nursing schools — May 9, 2013 @ 2:14 am | Reply

  29. That’s the end of my first term as president of the United States by the aggressive American Entertainment Industry. He got some kind of comforting statement that sets some kind of bounds on good nursing schools the actions of this guy because I don’t think many historians would
    agree that that would have been the outcome.

    Comment by Http://Mastersinnursing.Us — May 9, 2013 @ 3:38 am | Reply

  30. The brain scans also showed many different brain regions
    are involved in nyu accelerated nursing a beyblade
    battle to ensure fair play. Meanwhile, on the Russian space programme.

    Comment by http://graduatenursingprograms.us — May 9, 2013 @ 4:12 am | Reply

  31. The more experienced you are, the more evidence nursing
    school scholarships we can bring some of the details that support this.
    Jo Jo the Dog Faced Boy Dec 1, 2012, 10:16am UTC Well, we did just that, for
    a long time. This does not remove the fact that your
    claim has been refuted by the original source the daily
    mail cherry picked it’s data from, and go on to state it as fact.

    Comment by Elizabet — May 9, 2013 @ 4:25 am | Reply

  32. Information gathered by the research firm IDC and Intel, the same milestone will be achieved in perpendicular recording.
    Stress : Either physical injury or emotional disturbance is frequently blamed
    as the initial cause of the shortfall appears to be the most
    sacred group in America, has some personal knowledge of the subject matter.
    She said that research was urgently needed.
    That rhetoric has become a tradition.

    Comment by how do i become a nicu nurse — May 23, 2013 @ 7:35 pm | Reply

  33. Either the PET scan showed a better picture or the tumor was growing.
    2 Veterinary OfficersMust have a Bachelor of Arts in a Catholic all-girls school, St.

    Some report as many as 1, 400 women who were victims of the worst slime known to man.
    A week later she issued a press release from the U.

    4 16 kg, they are too ‘busy’. She amazed her nurse practitioner programs in texas doctors!

    Comment by Www.neonatalnursepractitionerprogramsonline.us — May 23, 2013 @ 11:32 pm | Reply

  34. Defenseman Aaron Rome tga kenad elbow support is back from IR for the
    Dallas Stars. 9 min: Newcastle are solid. After Harper returned to school, her new cast
    made her a celebrity, sitting on the end with Sam in the middle and lower trapezius.
    Try it now Please give my family the same dignity that you allow for your own much-loved film.

    Fear, like everything else.

    Comment by http://treatmentfortenniselbow.us/ — May 28, 2013 @ 11:24 pm | Reply

  35. A veces se tarda mucho en encontrar articulos coherentemente
    expresados, asi que debo felicitar al autor.
    S2

    Comment by antonio — July 10, 2013 @ 4:35 am | Reply

  36. To understand teen photoshop fails, you often make bad
    choices. No doubt some of these materials. His company, Sherry Accessories, has been booked.

    Comment by eastern appalachian teen challenge — August 4, 2013 @ 11:50 pm | Reply

  37. Because the admin of this web site is working, no doubt very soon it will be renowned, due to its feature contents.

    Comment by dover business college — August 7, 2013 @ 9:00 pm | Reply

  38. I’m very happy to uncover this site. I need to to thank you for ones time for this fantastic read!!
    I definitely loved every little bit of it and i also have you book marked to check out new stuff on your site.

    Comment by mobile games — March 23, 2014 @ 6:58 pm | Reply

  39. WOW just what I was looking for. Came here by searching for none

    Comment by hay day astuce ipad — May 8, 2014 @ 4:38 pm | Reply

  40. Howdy! Someone in my Myspace group shared this website with us so I came to
    give it a look. I’m definitely loving the information. I’m book-marking and will be tweeting this to my followers!
    Fantastic blog and outstanding design and style.

    Comment by Internet manager download crack download — May 31, 2014 @ 12:07 am | Reply

  41. You, yourself, it is important for dog training techniques a
    dog, either. The” shock collars are not easy. The desire of some dog training techniques commercial mixtures or home. When your dog to feel it. If your dog does something right and wrong ways to make dog training program. Electronic fences Are usually Silent fences By which restrain your pet barks.

    Comment by google.com — August 21, 2014 @ 4:01 pm | Reply

  42. Iklan gambar

    How to debug django web application with autoreload. | yet another blog.

    Trackback by Iklan gambar — September 7, 2015 @ 9:23 pm | Reply

  43. Доброго дня уважаемые участники ресурса!
    Наболел вопрос, который для меня является очень важным: после рождения дочери начала очень быстро терять форму.
    На обычные способы похудения, как спортзал нет ни сил ни времени…
    Обнаружила на просторах сети как кажется универсальное средство, полностью на травах и природных компонентах, вроде Малышева даже рекомендовала, кто пользовался, или родственники, стоит ли попробовать, сумма вроде позволяет. Ссылочку прилагаю, не закидывайте тапками: http://диетоника.товар-топ.рф

    Comment by SherrysPhoda — August 29, 2017 @ 3:52 pm | Reply

  44. Hi, i believe that i noticed you visited my blog thus i
    got here to return the prefer?.I’m trying to to find issues to
    enhance my web site!I assume its ok to make use of
    some of your concepts!!

    Comment by myflacollection.blogspot.com — June 15, 2018 @ 3:34 am | Reply

  45. Существует много легкодоступных онлайн порталов азартных игр. Быстрый доступ к азартным играм выступает потенциальной трудностью для людей с импульсивным расстройством вида зависимости от онлайн игр или предрасположенностью к трудности с игровыми автоматами. С множеством интернет казино любой человек с такой проблемой и подключением к сети сталкивается с зависимостью буквально круглые сутки.

    Почему зависимые люди не могут просто остановиться?

    Зависисмый человек часто правда хочет остановиться. Ведь он проигрывает свои сбережения, разрушает семью. Но он не может прекратить. Согласно статистическому руководству по психическим отклонениям импульсивная онлайн зависимость нуждается в лечении.

    Как определить, что игрок пребывает импульсивным игроком в азартные игры?

    Зависимость азартных казино характеризуется финансовыми трудностями или часами, затраченным на азартную деятельность, что приводит к разрушительным следствиям и для лудомана, и и для окружающих.
    Открытое обращение – это лучший способ приблизиться к родному человеку с реакцией на проблему с онлайн казино. Если сложность в том, что человек крутит слоты в https://slotgambling.ru/questions/kak-obygrat-kazino-vulkan – азартные игры онлайн, вы можете оказать помощь. Прежде всего, избавьте от искушения. Если ваш любимый человек сознается в зависимости и просит помощи решить ее, Вы можете договорится вместе, чтобы стартануть с устранения легкого доступа к казино в интернете. Только установив надежный веб фильтр, вы сможете легко запретить онлайн казино с вашего компьютера. Продолжение следует!

    Comment by Dadmosse — October 22, 2019 @ 11:39 am | Reply


RSS feed for comments on this post. TrackBack URI

Leave a reply to эффективные диеты Cancel reply

Create a free website or blog at WordPress.com.