1

Is there a way to see the details of the execution of a python file? I know that you can use logging, but obviously it would be unreasonable to set up the number of logs needed to see every line that's eventually executed. The python programme, of course, goes through the programme execution on a line by line basis, so to me, there must be a way to see the details of at the very least what line is being executed, and possibly where from.

I imagine that something like this would be possible:

lineno (27) 'test_file'
lineno (54) 'test_file'
lineno (12) 'test_file'
lineno (13) 'test_file'
Jim Jam
  • 713
  • 4
  • 10
  • 22
  • 2
    you can use a modern ide and then start a debugging session, so you can set checkpoints and see the state of each component in a specific line. – marcos Mar 09 '20 at 00:07
  • https://www.jetbrains.com/help/pycharm/stepping-through-the-program.html – Marcin Orlowski Mar 09 '20 at 00:08
  • The `pdb` command line debugger is usually included with the distribution. Profiling is another option. See https://docs.python.org/3/library/debug.html#debugging-and-profiling. There are multiple IDE's out there that do the same thing. – tdelaney Mar 09 '20 at 00:14
  • Does this answer your question? [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – kaya3 Mar 09 '20 at 00:15
  • Thank you' i'll give that a try :) – Jim Jam Mar 09 '20 at 00:20
  • @MarcinOrlowski Thanks, man. Worked like a charm. It's built-in and it works. The only thing I'd add is that it won't work until you set checkpoints by clicking to the right of the lineno, where a big red dot will appear indicating a breakpoint. – Jim Jam Mar 09 '20 at 00:27
  • That's how it works. If you want to steo from beginning, set the breakpoint at the very first line executed and step from there. – Marcin Orlowski Mar 09 '20 at 00:35
  • @MarcinOrlowski You should honestly provide this as an answer because if you do I'll accept it :) – Jim Jam Mar 09 '20 at 00:37
  • Does this answer your question? [How to step through Python code to help debug issues?](https://stackoverflow.com/questions/4929251/how-to-step-through-python-code-to-help-debug-issues) – AMC Mar 09 '20 at 00:41
  • It seems, that Marcin doesn't want to post his comments as an answer. Perhaps you could answer the question yourself and mention Marcin. So others have it easier when going through this question to find out what helped you. – gelonida May 06 '20 at 10:35

2 Answers2

1

What you want is a debugger.

There are several options, such as:

CherryDT
  • 25,571
  • 5
  • 49
  • 74
1

Debuggers are probably the better answer but you might also just use the trace module ( https://docs.python.org/3.6/library/trace.html ):

python -m trace -t yourscript.py

will display all lines your program is executing. If you just want to see how python steps through one script you could do.

python -m trace -t yourscript.py | grep yourscript.py

Please check following example and the output. Contents of tst.py:

sum = 0
for i in range(4):
    sum += i
print(sum)

And now the output you get:

$ python -m trace -t tst.py
 --- modulename: tst, funcname: <module>
tst.py(1): sum = 0
tst.py(2): for i in range(4):
tst.py(3):     sum += i
tst.py(2): for i in range(4):
tst.py(3):     sum += i
tst.py(2): for i in range(4):
tst.py(3):     sum += i
tst.py(2): for i in range(4):
tst.py(3):     sum += i
tst.py(2): for i in range(4):
tst.py(4): print(sum)
6
 --- modulename: trace, funcname: _unsettrace
trace.py(77):         sys.settrace(None)

gelonida
  • 5,327
  • 2
  • 23
  • 41
  • Trace doesn't do anything though – Jim Jam Mar 09 '20 at 00:29
  • you must be more specific of how you tried to reproduce. Please look at my enhanced answer. – gelonida Mar 09 '20 at 00:32
  • Yeah, it still doesn't do anything. I'm running it from the terminal by the way. IDE is Pycharm. Thanks though. – Jim Jam Mar 09 '20 at 00:36
  • Try to run it from a CMD window (if running under windows) or from a console window if under an OS like Linux / Unix / MacOS If you get this working others might help you to get it running under PyCharm. I don't use it and don't know it well enough. So can't really help there – gelonida Mar 09 '20 at 00:40