debugger.py 796 B

12345678910111213141516171819
  1. import os
  2. def setup_debugger(*, host: str = "0.0.0.0", port: int = 5678):
  3. """Configure debugging via debugpy."""
  4. # Only to be used in development. Should someone inadvertently set DEBUG to True in
  5. # staging or production, a ModuleNotFoundError will be raised, because debugpy is
  6. # only available via requirements-dev.txt - this is intentionally.
  7. if os.environ.get("DEBUG") or os.environ.get("DEBUG_WAIT_FOR_CLIENT"):
  8. try:
  9. import debugpy
  10. debugpy.listen((host, port))
  11. if os.environ.get("DEBUG_WAIT_FOR_CLIENT"):
  12. print("🔌 debugpy waiting for a client to attach 🔌", flush=True)
  13. debugpy.wait_for_client()
  14. except (ModuleNotFoundError, RuntimeError) as e:
  15. print(e, flush=True)