I use this snippet to get a debugging session in Python code. There are alternatives, like using a Jupyter notebook, or the debugging tools in your IDE, but this is often easier since it doesn’t require any refactoring or configuration.

Here’s the snippet:

import code
code.interact(local=dict(globals(), **locals()))

This imports the code library, then launches an interactive session with access to any local and global variables at the point where code.interact() is called.

If you’re using VSCode, you can add this as a snippet:

Cmd+Shift+P Snippets: Configure User Snippet

Then choose a snippet file and insert the following:

{
// your other snippets
	"debug": {
	    "prefix": "debug",
	    "body": [
			"import code",
			"code.interact(local=dict(globals(), **locals()))"
	    ],
	    "description": "Debug breakpoint using code.interact()"
	},
}

I got this from somewhere on Twitter, I believe it was Andrej Karparthy but not sure.