debugpy - a debugger for Python
This debugger implements the Debug Adapter Protocol: debugProtocol.json
debugpy Import usage
At the beginning of your script, import debugpy, and call debugpy.listen() to start the debug adapter, passing a (host, port) tuple as the first argument.
As with the --listen command line switch, hostname can be omitted, and defaults to "127.0.0.1":
Waiting for the client to attach
Use the debugpy.wait_for_client() function to block program execution until the client is attached.
debugpy.wait_for_client() # blocks execution until client is attached
In Python 3.7 and above, debugpy supports the standard breakpoint() function. Use debugpy.breakpoint() function for similar behavior and compatibility with older versions of Python. If the debugger is attached when either of these functions is invoked, it will pause execution on the calling line, as if it had a breakpoint set. If there's no client attached, the functions do nothing, and the code continues to execute normally.
breakpoint() # or debugpy.breakpoint() on 3.6 and below
To enable debugger internal logging via CLI, the --log-to switch can be used:
-m debugpy --log-to path/to/logs ...
When using the API, the same can be done with debugpy.log_to():
In both cases, the environment variable DEBUGPY_LOG_DIR can also be set to the same effect.
When logging is enabled, debugpy will create several log files with names matching debugpy*.log in the specified directory, corresponding to different components of the debugger. When subprocess debugging is enabled, separate logs are created for every subprocess.