Django debugging with Emacs

I found a lot of info about how to debug simple Python programs with Emacs. But what if I want to debug a Django application? I run the development server and I would like to somehow attach to the process from Emacs and then set breakpoints, etc. Similar to Visual Studio's "attach to process". How to do that?


Asked by: Sarah376 | Posted: 24-09-2021






Answer 1

This isn't emacs specific, but you can use the Python debugger by adding the following to a Django view function:

import pdb; pdb.set_trace()

Now when you run the development server and view the page, your browser will appear to hang or load very slowly - switch over to your console, and you have access to the full debugger. You can inspect AND modify state of your application via an interactive shell - check out the Python documentation for the debugger, or this link for some Python debugging examples


If all you need is logging, add the following to your settings.py:

logging.basicConfig(
    level = logging.DEBUG,
    format = '%(asctime)s %(levelname)s %(message)s',
    filename = '/tmp/mylog.log',
    filemode = 'w'
)

Now you can log messages to /tmp/mylog.log by adding the following to any view function:

import logging
logging.debug("Something happened")

Answered by: Caroline110 | Posted: 25-10-2021



Answer 2

Start pdb like this:

M-x pdb

Then, start the Django development server:

python manage.py runserver --noreload

Once you have the (Pdb) prompt, you need to do this:

import sys
sys.path.append('/path/to/directory/containing/views.py')

Once you've done this, you should be able to set breakpoints normally. Just navigate to the line number you want, and

C-x SPC

Answered by: Lily707 | Posted: 25-10-2021



Answer 3

Here's something I found last night that will do exactly what you want when the program crashes:

http://code.google.com/p/django-command-extensions/

Once you install that you can run:

python manage.py runserver_plus

and you will have an interactive AJAX console on your Error page. (Obviously, be careful with the amount of access people have to this web server when running in that mode.)

GitHub: https://github.com/django-extensions/django-extensions

You can get Django Extensions by using pip or easy_install:

$ pip install django-extensions or $ easy_install django-extensions

If you want to install it from source, grab the git repository from GitHub and run setup.py:

$ git clone git://github.com/django-extensions/django-extensions.git
$ cd django-extensions
$ python setup.py install

Answered by: Eric367 | Posted: 25-10-2021



Answer 4

Because recent versions of Emacs python mode do support 'pdbtrack' functionality by default, all you need is just set up breakpoint in your code this way:

import pdb; pdb.set_trace()

Also, you have to start your Django application devserver from within Emacs shell:

M-x shell

And then, in the shell, start the Django development server:

python ./manage.py runserver

P.S. No need for specific pdb sessions or --noreload flag. Noreload would require you to manually restart your applications and so I do not find this useful for Emacs.

Answered by: Justin794 | Posted: 25-10-2021



Answer 5

I don't really know anything about it, but putting "debugging Python with emacs" into Google gave me this page about debugging twisted with emacs, so it seems to be possible.

Answered by: Kelvin548 | Posted: 25-10-2021



Answer 6

About the general non-emacs-exclusive way, there is a very nice screencast out there you might be interested in: http://ericholscher.com/blog/2008/aug/31/using-pdb-python-debugger-django-debugging-series-/

The emacs integration described above doesn't work for me yet. It doesn't really seem to connect to the running application.

Further I consider this blog post here very interesting: http://web.archive.org/web/20101230072606/http://panela.blog-city.com/python_and_emacs_5_pdb_and_emacs.htm

cu Roman

Answered by: Roman902 | Posted: 25-10-2021



Similar questions

python - django debugging help

I am new to python, django and wing ide. I come from .net. I need to debug a django web app and I am not sure where the entry point is so I can start debugging right from the beginning. Please advise. Does the app have to be in the mysites folder i configured during the configuring django? Or it can be anywhere? I have init, main, manage, settings, settings_local and urls files in the init...


python - Debugging slow Django Admin views

Closed. This question needs to be more focused. It ...


python - Debugging Django admin panel bug

When I navigate to the admin interface for a particular model (the whole-table view), and simply hit 'Save', this error was popping up on the usual red banner: Please correct the errors below. Needless to say I couldn't actually make edits from this view, until I experimented and fixed it. Here's my class: class RoleMapping(models.Model): MIN_LENGTH, MAX_LENGT...


python - Debugging Django in VSCode error

I try to debug my Django app in VSCode my launch.json settings: { "name": "Python: Django", "type": "python", "request": "launch", "stopOnEntry": false, "pythonPath": "D:\\software\\program_books\\python\\exercises\\mezzanine\\env\\Scripts\\python.exe", "program": "${workspaceFolder}\\mezzanin...


debugging - What are good ways to make my Python code run first time?

Closed. This question needs to be more focused. It ...


linux - Python memory debugging with GDB

We have a Linux application that makes use of OpenSSL's Python bindings and I suspect it is causing random crashes. Occasionally, we see it crash with the message: Python Fatal Error: GC Object already tracked which would appear to be either a programming error on the part of the library, or a symptom of memory corruption. Is there any way to know the last line of Python source...


python - Debugging a pyQT4 app?

I have a fairly simple app built with pyqt4. I wanted to debug one of the functions connected to one of the buttons in my app. However, when I do the following python -m pdb app.pyw > break app.pyw:55 # This is where the signal handling function starts. things don't quite work like I'd hope. Instead of breaking in the function where I've set the breakpoint and letting me step through i...


Python Debugging

Closed. This question does not meet Stack Overflow guid...


debugging - check what files are open in Python

I'm getting an error in a program that is supposed to run for a long time that too many files are open. Is there any way I can keep track of which files are open so I can print that list out occasionally and see where the problem is?


python - PyQt debugging in main loop

Can I debug PyQt application when is main loop running ? Pdb, NetBeans, PyDev, all "freeze" when sys.exit(app.exec_()) is executed. I probably missing something obvious. Or what can be problem, please ? I apologize for my "creepy" english. Thanks.


linux - Debugging Python Crash

I am building Python 2.6 4 from source on a Linux server and am experiencing a Segmentation Fault when running the tests (make test) (test_hashlib.py and test_hmac.py). When I opened the core dump file in gdb, I am told that the error is at 0x00002b73379ac446 in ??. I then recompiled python with both my CFLAGS and CPPFLAGS set to -g to enable debug symbols and reran the failing test. When I opened the core ...


debugging in python

i am using the python shell to try to do debugging i set a breakpoint i did: >>> import pdb >>> import mymodule >>> pdb.run('mymodule.test()') but it is just running my program without stopping at the breakpoint! what am i donig wrong?


python - Debugging SCons

I've written a rather complicated set of SCons scripts, and I have some errors I cannot find, for example intermediate files are removed or there are cyclic dependencies reported, and I want to know the reason. Unfortunately, I don't see any means to get good verbose debugging output of what SCons is doing and where. For example, the option --taskmastertrace=FILE does not explain why my intermediate files were...


python - Debugging sample Pygame code?

A simple question: Why does this the first code work but the seemingly identical second code freezes up when the pygame window comes? # Moving Pan # Demonstrates mouse input from livewires import games games.init(screen_width = 640, screen_height = 480, fps = 50) class Pan(games.Sprite): """ A pan controlled by the mouse. """ def update(self): """ Move to mouse coordinates. """ se...


python - Debugging pylons in Eclipse under Ubuntu

I am trying to get pylons to debug in Eclipse under Ubuntu. Specifically. I am not sure what to use for the 'Main Module' on the Run configurations dialog. (this is a similar question on stackoverflow, but I think it applies to windows as I can't find paster-script.py on my system) Can anyone help?


debugging - When to use the Python debugger

Since Python is a dynamic, interpreted language you don't have to compile your code before running it. Hence, it's very easy to simply write your code, run it, see what problems occur, and fix them. Using hotkeys or macros can make this incredibly quick. So, because it's so easy to immediately see the output of your program and any errors that may occur, I haven't uses a debugger tool yet. What situations may call ...


debugging - What are good ways to make my Python code run first time?

Closed. This question needs to be more focused. It ...


linux - Python memory debugging with GDB

We have a Linux application that makes use of OpenSSL's Python bindings and I suspect it is causing random crashes. Occasionally, we see it crash with the message: Python Fatal Error: GC Object already tracked which would appear to be either a programming error on the part of the library, or a symptom of memory corruption. Is there any way to know the last line of Python source...


Debugging a running python process

Is there a way to see a stacktrace of what various threads are doing inside a python process? Let's suppose I have a thread which allows me some sort of remote access to the process.


debugging - How do you pass script arguments to pdb (Python)?

I've got python script (ala #! /usr/bin/python) and I want to debug it with pdb. How can I pass arguments to the script? I have a python script and would like to debug it with pdb. Is there a way that I can pass arguments to the scripts?


debugging - Stop python from closing on error

In python when running scripts is there a way to stop the console window from closing after spitting out the traceback?


django - Python: Memory leak debugging

I have a small multithreaded script running in django and over time its starts using more and more memory. Leaving it for a full day eats about 6GB of RAM and I start to swap. Following http://www.lshift.net/blog/2008/11/14/tracing-python-memory-leaks I see this as the most common types (with only 800M of memory used):


debugging - Compiling python modules with DEBUG defined on MSVC

Python rather stupidly has a pragma directive in its include files that forces a link against python26_d.lib when the DEBUG preprocessor variable is defined. This is a problem because the python installer doesn't come with python26_d.lib! So I can't build applications in MSVC in debug mode. If I temporarily #undef DEBUG for just one file I get many complaints about incons...


debugging - Python code seems to be getting executed out of order

At work I have a programming language encoded in a database record. I'm trying to write a print function in python to display what the record contains. This is the code I'm having trouble with: # Un-indent the block if necessary. if func_option[row.FRML_FUNC_OPTN] in ['Endif', 'Else']: self.indent = self.indent - 1 # if this is a new line, indent it. if len(self.formulatext...






Still can't find your answer? Check out these communities...



PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python



top