Verified Commit 0a87b856 authored by Renato Alves's avatar Renato Alves 🌱
Browse files

Add notes on pdb and local and remote debugging with vscode

parent 8d6b1425
# Debugging in Python
## Some concepts
* *Breakpoint* - location in a file where the debugger should pause
* *Continue* - resume execution until next breakpoint or unexpected condition
* *Step over* - execute the current line and pause at the beginning of the next
* *Step into* - if the current line is a function call, enter the function and execute from the first line of the function
* *Step out* - execute the current function till the end and pause as soon as the function returns
* *Backtrace* - the execution path up to the location where the debugger is paused
## `pdb` cheat sheet
* `args` - show arguments from surrounding function
* `b` & `break` - set or list breakpoints
* `display` - show tracked variables
* `n` & `next` - execute instruction in next line (step over)
* `s` & `step` - execute next possible instruction (step into)
* `r` & `return` - execute until end of current function (step out)
* `p` & `pp` - print result of expression
* `ll` & `longlist` - show the source code/context of current paused state
* `bt` & `w` & `where` - show the execution path up to the current location
## launching pdb
Add `import pdb ; pdb.set_trace()` to your code and you will get access to `pdb` on the spot.
Alternatively run `python -mpdb` from the command-line.
## VSCode / VSCodium
Install [Python extension]( by Microsoft to have Python goodies and debugging powers.
## Remote debugging & development
The [remote development pack]( is pretty awesome. You might find the [Remote - SSH]( option very useful, together with the [Remote - Containers]( if you work with a lot with docker and similar solutions.
### Reaching the location where the code is running
This requires some familiarity with network concepts and `SSH`.
You will need to setup port-forwarding (aka tunneling) using `SSH`.
You might also need to `ProxyJump` a few computers to get to the location where the code is running.
A simple, "one hop" `SSH` tunnel can be achieved with `ssh -L localhost:5678:localhost:5678`.
For more about SSH tunnels try [these docs]( and [how to use `ProxyJump`](
### debugpy
`debugpy` is [pretty awesome]( but needs to connect via the network. You'll need to use the `SSH` tunnel setup above.
\ No newline at end of file
import os
import glob
def split(path):
return path.rsplit("/", 1)
def get_path(filename):
head, tail = split(filename)
return os.path.abspath(head)
def main(hostname):
extension = "one/**/*.py"
files = glob.glob(extension, recursive=True)
path = get_path(files[0])
print(">>>>", path, "<<<< on", hostname)
if __name__ == "__main__":
hostname = os.uname().nodename
if hostname == "":
import debugpy
print("debugpy loaded and waiting for connection")
print("debugpy session active")
\ No newline at end of file
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment