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