Debugging¶
In case something does not work the way you want there are several ways to debug things.
Viewing module contents¶
We recommend to create a simple file with just the part that does not work.
We usually call this file ex.py
and remove it before the actual commit.
To reveal internals of this Python source code use:
python3.12 -m ast ex.py
python3.12 -m tokenize ex.py
It might not be enough to find some complex cases, but it helps.
Test-driven development¶
A lot of people (including @sobolevn) finds test-driven development really useful to design and debug your code.
How?
Write a single test that fails for your new feature or exposes a bug
Run it with
pytest tests/full/path/to/your/test_module.py
Use the magic of
print
andast.dump
to view the contents of nodesFix the bug or implement a new feature
Make sure that everything works now: tests must pass
Done!
Interactive debugging¶
We recommend to use ipdb
for interactive debugging
(it is already included as a development package to this project).
To start interactive debugging session you will need to:
Set
export PYTHONBREAKPOINT=ipdb.set_trace
environment variablePut
breakpoint()
call in places where you need your debugger to stopRun your program as usual, debugger will stop on places you marked
This way allows to view local variables, execute operations step by step, debug complex algorithms.
Visual debugging¶
One can use vscode
or pycharm
to visually debug your app.
In this case you need to setup appropriate entrypoints
and run your app in debug mode.