6 ms·
Django: Fix a view using a debugger with breakpoint()
- agumonkey 2y agoIs there a proper way to do this in a docker container ?
- kissgyorgy 2y agoYou can start the container with docker run -it so it has a tty
- abalaji 2y agoanother option from the sibling comment is to use `remote-pdb` which you can then telnet into from outside the container after exposing the port
- agumonkey 2y agooh nice
- senkora 2y agoI've shared this on here before, but another trick that I like with breakpoint() is to add this function to my Python projects so that I can drop into pdb by sending a signal: import signal def sigusr1_handler(signum, stack): breakpoint() signal.signal(signal.SIGUSR1, sigusr1_handler) And then I can drop into pdb by sending a signal: $ kill -s sigusr1 $PID It's especially useful for getting a backtrace out of your program when it's hanging, and poking around a bit to figure out why.
- misnome 2y agoIt doesn’t give you a debug repl, but my favourite tool for this sort of thing is py-spy; small and easy enough to install into a stuck pod’s ephemeral storage and and it can dump the stacktrace or give you a top-equivalent view on what your python process is doing. I was afraid it died as it didn’t get updated for python 3.12, but that’s thankfully apparently been solved now!
- randlet 2y agoNice trick!
- tmnvix 2y agoAlso useful is adding a breakpoint in a template using a custom template tag. @register.simple_tag(takes_context=True) def set_breakpoint(context, *args): vars = [arg for arg in locals()["args"]] breakpoint() Then add the breakpoint in your template wherever you like with {% set_breakpoint %}
- deleted 2y ago[deleted]
- funkydata 2y agoNot trolling, but what's wrong with visual remote debugging using either Pycharm or Visual Studio? I find it much more rich information wise than using pdb.
- ashconnor 2y agoCan you set breakpoints within templates using PyCharm?
- funkydata 2y agoYes, you can. It has to be where there is templating language of course (you cannot put a breakpoint on a arbitrary div for example), you can also debug the rendering of the template through Pycharm if you want to in order to have a full stack debug, but I'm used to doing that using the browser developer tools.
- ranger_danger 2y agoWould be nice if there was a way to debug a view remotely in production.
- funkydata 2y agoWell, as previously stated in Pycharm and Visual Studio (not tested on others) there is. If I remember correctly, both will install their requirements remotely and then allow you to do so. However, I would not recommend it: - you will probably forget to clean up after the fact leaving you with both performance and security problems - you should never debug in production, use something like Sentry instead also, better to replicate the production environment in a sandbox (either on your machine using Vagrant or Docker, etc. if feasible) or in the cloud by taking a snapshot of your production environment and using it as a sandbox to debug (and then of course destroying it after use).
- djaouen 2y agoMy coworker used to add `assert False`s into the view so that, when the stacktrace info page comes up (Django still has these, right?), you would have access to all the locals. It's been a while since I've used Django for a project, and I am still on-the-fence about such usage lol. But it worked.