Debugging A Running Python Application post
Spoiled by Xdebug?
For all the insults that PHP catches, one that seems to never come up is how good Xdebug is for figuring out what might be going on in an application.
I have been porting over a Python web app that is used to trigger a batch processing job that validates online purchases. I have been rewriting parts of it in PHP + Laravel + queue workers and as part of the testing, I was trying to figure out why I could not trigger the batch processing properly.
I have been spoiled by being able to use Xdebug to figure out what is going on with my PHP applications, but had never used Python's debugger.
Since I am a happy PhpStorm user I figured I should also use PyCharm to do my Python dev work. I figured they were similar enough that it wouldn't be painful to debug.
As an aside, learning how to use a debugger for your preferred programming
language will give you real productivity gains. No more die('here')
statements
in your code.
How does this work in Python?
So, having no experience with Python debugging I ass-u-me'd that it would be behaving similar to how Xdebug does things:
- make sure you have a version of Python with debugger support
- it sends messages out on a specific port
- tell the IDE to listen
- set your breakpoint
- wait for the nice debugger output to come up
Well, dear friends, this is not how it worked at all. I was stumped and wondering "why doesn't it work like Xdebug?!?". Which is, of course, silly. It was just my assumptions that were wrong.
I spent some time doing online searches with all sorts of variations on how to debug Python apps in PyCharm. Lots of interesting and helpful blog posts...none of which covered what I was trying to do.
I asked on federated social media for some help -- again, some helpful advice but I got variations of "don't try it that way". Now, that is well-intentioned but not really an answer.
I had a web app, running locally inside a Python virtual environment. This shouldn't be hard. I was starting to think that the issue was that I was not using the same terms as everyone else.
With a running app, I kept wondering "so how do I continually restart the application to turn on the debugger" as that seemed to me at the time the only solution for the issue.
Clearly, I was doing it wrong.
While doing some more random searches, I finally stumbled across something that turned out the be the solution. In a blog post (sorry, I did not bookmark it) about debugging another Python web application the person said "...and just attach the debugger to a running process".
Apologies to people who might not get the reference, but if you've ever seen an image of a person thinking about something and then a light bulb goes on over head? That is what it was like.
Debugging by attaching to a running process
Keep in mind that these details are while using PyCharm 2024.1.4 (Professional Edition). So, in my situation I had this:
- project is configured in PyCharm to use the Python interpreter in my virtual environment
- the application is running
- I have set a breakpoint in my Python code
Inside PyCharm I went to Run > Attatch to Process
and was presented with
some Python processes along with their process ID (I'm on MacOS). I selected the
one for my application and then made my request to the web app to turn
start the batch processing and it worked! I could now
debug things inside and figure out how things are working.
Hope this helps folks who are less-familiar with Python and it's debugging capabilities get up to speed faster.
Categories: development