Debugging Raspberry Pi Python code using VS Code

  1. Assuming following recommended installation guides - code already installed on Pi, and both Pi & local PC connected to the same WiFi network.
  2. In Pi terminal type ifconfig and get the IP address (might be changed on each reboot).
  3. In VS Code, connect to host - remote ssh. Use Pi’s IP address, user name & password.
  4. In VS Code, open “Explorer” view from the “Activity Bar” and open the relevant folder (on Pi) with the code.
  5. In VS Code, open “Run & Debug” view from the “Activity Bar”, and add new configuration to launch.json:
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Regular Debug",
            "type": "debugpy",
            "request": "launch",
            "program": "${file}",
            "args": ["--input", "rpi"]  // optional
        }
    ]
}
  1. While active open file (because “program” param set to ${file}) is the Python application, for example “detection.py”, run the configuration. This will try to debug the code but will show some error, while a Python debug terminal will open representing Pi remote folder with the code.
  2. In that terminal execute:
cd hailo-rpi5-examples
source setup_env.sh  # or “. s” and hit tab
export DISPLAY=:0  # since current active display is PC and Pi connected remotely via SSH
  1. In the Python application code, for example “detection.py”:
import debugpy
def app_callback(pad, info, user_data):
    debugpy.listen(("127.0.0.1", 5688))  # port number meaningless
    debugpy.wait_for_client()
    breakpoint()  # or wherever actual break point required
    ...
  1. While active file is the Python application, for example “detection.py”, Run the configuration. This time it will debug and stop at the breakpoint.
1 Like