Skip to content
Snippets Groups Projects
Verified Commit 0a87b856 authored by Renato Alves's avatar Renato Alves :seedling:
Browse files

Add notes on pdb and local and remote debugging with vscode

parent 8d6b1425
No related branches found
No related tags found
No related merge requests found
# 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 script.py` from the command-line.
## VSCode / VSCodium
Install [Python extension](https://marketplace.visualstudio.com/items?itemName=ms-python.python) by Microsoft to have Python goodies and debugging powers.
## Remote debugging & development
The [remote development pack](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.vscode-remote-extensionpack) is pretty awesome. You might find the [Remote - SSH](https://aka.ms/vscode-remote/download/ssh) option very useful, together with the [Remote - Containers](https://aka.ms/vscode-remote/download/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 login.cluster.embl.de`.
For more about SSH tunnels try [these docs](https://www.ssh.com/ssh/tunneling/example) and [how to use `ProxyJump`](https://en.wikibooks.org/wiki/OpenSSH/Cookbook/Proxies_and_Jump_Hosts#Passing_Through_One_or_More_Gateways_Using_ProxyJump).
### debugpy
`debugpy` is [pretty awesome](https://github.com/microsoft/debugpy) 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 == "login.cluster.embl.de":
import debugpy
print("debugpy loaded and waiting for connection")
debugpy.listen(12125)
debugpy.wait_for_client()
print("debugpy session active")
main(hostname)
\ No newline at end of file
../../../../../debugging.py
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment