Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I trace my python program from start of execution to finish?

I have a function written in python, and I want to inspect how it runs step by step from start to finish. How do I go about doing this?

I am using PyCharm as an IDE, but I don't know if it has a tracing feature.

Any tips or resources that are newbie friendly on this issue?

Thanks very much in advance!

like image 392
eAx Avatar asked Jan 25 '26 16:01

eAx


2 Answers

What you're looking for is a profiler. Luckily, PyCharm is really powerful and comes with a wealth of debugging/profiling tools.

like image 151
ospahiu Avatar answered Jan 28 '26 07:01

ospahiu


If you're running your code within PyCharm, simply set a breakpoint on the first line within the function you wish to examine, then step through it using their interface.

If you're running your code via the commandline, I highly recommend familiarizing yourself with Python's debugging module, pdb. All you need to do to examine your function is temporarily add the line:

import pdb;pdb.set_trace()

.. as the first line of your function. When you run it and it hits this line, you can step through the execution on the commandline using simple directives like 'n' for next line.

like image 22
lukewarm Avatar answered Jan 28 '26 09:01

lukewarm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!