Skip to content

python_general

Marcel Schmalzl edited this page Feb 19, 2025 · 4 revisions

Python General

Using the Pdb debugger

Run the Pdb debugger

python -m pdb main.py 

Halt at beginning of execution

export PYTHONBREAKPOINT=pdb.set_trace python main.py

Break at exception

import sys, pdb; sys.excepthook = lambda exctype, value, tb: (print("Unhandled exception detected. Entering debugger..."), pdb.post_mortem(tb))

Set breakpoint

breakpoint()

Useful commands

To be used within a debug prompt.

Breakpoint navigation

# Continue exec until next breakpoint continue # Step over (exec line but don't step into func) next # Step into (enter func & stop at 1st line inside it) step # Return from current func return

Break at file::line

break relative/path/to/script.py:10

Note that you cannot break at comments / empty lines / ...

Break at function name

break my_func

Conditional breakpoints

break relative/path/to/script.py:10, x > 5

Disable n breakpoints

disable # same as: disable 1 # disable all disable all

You need to continue if you don't want to stay in the current break.

Inspect objects

Print the structure of a whole object via pprint.pprint(vars(...)):

import pprint # Object to inspect class PhysicalFunction: def __init__(self): self.name = "ExampleFunction" self.parameters = { "param1": 10, "param2": 20, "param3": [1, 2, 3, 4, 5], "param4": {"subparam1": "value1", "subparam2": "value2"} } self.description = ("This is a detailed description of the ExampleFunction. " "It includes multiple parameters and nested structures.") attributes = pprint.pprint(vars(PhysicalFunction()))

Output:

{'description': 'This is a detailed description of the ExampleFunction. It ' 'includes multiple parameters and nested structures.', 'name': 'ExampleFunction', 'parameters': {'param1': 10, 'param2': 20, 'param3': [1, 2, 3, 4, 5], 'param4': {'subparam1': 'value1', 'subparam2': 'value2'}}}

Python 3

(un)fold
Snippets
General
Libs

Linux/bash

(un)fold
Guides
Scripts

Git

(un)fold

C/C++

(un)fold

Video

(un)fold

Databases

(un)fold

Misc

(un)fold

Windows

(un)fold

Mac

(un)fold

SW recommendations

(un)fold

(Angular) Dart

(un)fold

Clone this wiki locally