1

In bash if one wants to see code execution of a script, one can place set -x -o verbose in the beginning of the code in the file.

Example.

set -x -o verbose

echo "testing"
...

Similar to this is there anything in python?

I have a python script I want to see all the code execution.

oguz ismail
  • 1
  • 16
  • 47
  • 69
Santhosh
  • 9,965
  • 20
  • 103
  • 243
  • 1
    See: [How to step through Python code to help debug issues?](https://stackoverflow.com/q/4929251/3776858) – Cyrus May 09 '21 at 07:52
  • 1
    anything like `set -x` in bash. `pdb` is more for debugging, but does not display the variables while executing – Santhosh May 09 '21 at 07:55
  • python doesnt run your lines directly it converts them to a more simplified language before executing (although it does contain all the necessary line number information still) if you're wondering about something like "where does my script spend most of it's time?" you can try using the default python profiler – AntiMatterDynamite May 09 '21 at 08:35
  • Since `__repr__()` implementations can have side effects, printing values could result in the execution path being different depending on whether tracing is active. (I've had that happen in practice). – Charles Duffy May 09 '21 at 19:29

1 Answers1

1

Python doesn't exactly give you an option that directly matches your requirements. However, there are two options that might come close.

PDB

As it's already been discussed in the comment section, you can run your script in pdb mode. Let's say you want to run the following script.py:

from __future__ import annotations
from typing import TypeVar

T = TypeVar("T")


def echo(thing: T) -> None:
    print(thing)


if __name__ == "__main__":
    echo("Something")

You can execute the script like this:

python -m pdb script.py

It should invoke the PDB prompt like this:

> /home/rednafi/workspace/personal/demo/script.py(1)<module>()
-> from __future__ import annotations
(Pdb) 

You can step through and execute the following lines by typing n:

> /home/rednafi/workspace/personal/demo/script.py(1)<module>()
-> from __future__ import annotations
(Pdb) n
> /home/rednafi/workspace/personal/demo/script.py(2)<module>()
-> from typing import TypeVar
(Pdb) n
> /home/rednafi/workspace/personal/demo/script.py(4)<module>()
-> T = TypeVar("T")
(Pdb) n
> /home/rednafi/workspace/personal/demo/script.py(7)<module>()
-> def echo(thing: T) -> None:
(Pdb) n
> /home/rednafi/workspace/personal/demo/script.py(11)<module>()
-> if __name__ == "__main__":
(Pdb) n
> /home/rednafi/workspace/personal/demo/script.py(12)<module>()

However, this doesn't print the values automatically. In this case, you'd have to either call the echo function or type continue in the terminal prompt.

Trace

Another option is to use the trace flag. Unlike PDB, this prints the output automatically. However, this is a lot more verbose compared to the first option.

python -m trace -t script.py
...
script.py(7): def echo(thing: T) -> None:
script.py(11): if __name__ == "__main__":
script.py(12):     echo("Something")
 --- modulename: script, funcname: echo
script.py(8):     print(thing)
Something

Redowan Delowar
  • 1,580
  • 1
  • 14
  • 36