I'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.
It 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!
My 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.
Not 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.
Yes, 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.
Well, 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).