Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save paxcodes/4385873618bf581f8159f052432373d8 to your computer and use it in GitHub Desktop.
Save paxcodes/4385873618bf581f8159f052432373d8 to your computer and use it in GitHub Desktop.

❓🐳 ⚙️ How to Debug Python App in VS Code

Context: Python app needs a database and our database is setup through Docker.


In VS Code docs,

  1. Add debugpy to your requirements.txt file:

✅ Done

  1. Add the following code snippet to the file that you wish to debug:
import debugpy  # noqa

debugpy.listen(("0.0.0.0", 5678))
debugpy.wait_for_client()

✅ I added the code at the end of main.py ⚠️ We have to also specify the host debugpy.listen(("0.0.0.0", 5678))

❓ What is the difference between 0.0.0.0 (the "host" that we need to specify to make it work) and 127.0.0.1 (the default)?

Read networking - What's the difference between 127.0.0.1 and 0.0.0.0? - Super User

Add a Python: Remote Attach configuration to launch.json in the .vscode folder: ...

✅ Done

Modify the docker-compose.yml file to expose the debugger port by adding 5678:5678 to the ports section. If you are using docker run to run your container from the terminal, you must append -p 5678:5678.

✅ I added "5678:5678" in backend's ports AND changed port setting in Traefik.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment