debugger.py 826 B

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