我正在尝试在附加模式下运行 debugpy 以在 VScode 上调试一个 dockerized Django 应用程序。在
launch.json
上具有以下配置
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Django",
"type": "python",
"request": "attach",
"pathMappings": [{
"localRoot": "${workspaceFolder}",
"remoteRoot": "/app"
}],
"port": 9999,
"host": "127.0.0.1"
}
]
}
我已经能够正确地附加到它,在
manage.py
文件中添加以下部分:
#!/usr/bin/env python
import os
import sys
from pathlib import Path
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.local")
# debugpy configuration
from django.conf import settings
if settings.DEBUG:
if os.environ.get("RUN_MAIN") or os.environ.get("WERKZEUG_RUN_MAIN"):
import debugpy
debugpy.listen(("0.0.0.0", 9999))
...
Django
Dockerfile
正在启动脚本:
#!/bin/bash
set -o errexit
set -o pipefail
set -o nounset
python manage.py migrate
exec python manage.py runserver 0.0.0.0:8000
我正在公开运行 docker 映像的端口 8000 和 9999。到目前为止一切顺利。
我现在要做的是为在 uvicorn 下运行的 ASGI 应用程序启用相同的支持。
#!/bin/bash
set -o errexit
set -o pipefail
set -o nounset
python manage.py migrate
exec uvicorn config.asgi:application --host 0.0.0.0 --reload --reload-include '*.html'
asgi.py
"""
ASGI config for Suite-Backend project.
It exposes the ASGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/dev/howto/deployment/asgi/
"""
import os
import sys
from pathlib import Path
from django.core.asgi import get_asgi_application
# This allows easy placement of apps within the interior
# suite_backend directory.
BASE_DIR = Path(__file__).resolve(strict=True).parent.parent
sys.path.append(str(BASE_DIR / "suite_backend"))
# If DJANGO_SETTINGS_MODULE is unset, default to the local settings
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.local")
# This application object is used by any ASGI server configured to use this file.
django_application = get_asgi_application()
即使强制 debugpy 监听端口 9999,我也无法附加到它。我无法为您提供任何日志,但我认为使用“Python:Django”配置运行 VSCode 调试器不能简单地找到可用的侦听器,然后就死掉了。
您对如何正确设置此环境有任何线索或建议吗?我已经搜索了大约一个小时,但找不到有关此事的任何资源。
您可以定义一个单独的环境变量来启用
debugpy
与 ASGI 像 IS_DEBUGPY_ATTACHED
可以是一个布尔值,然后修改您的 asgi.py
文件如下:
"""
ASGI config for Suite-Backend project.
It exposes the ASGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/dev/howto/deployment/asgi/
"""
import os
import sys
from pathlib import Path
from django.core.asgi import get_asgi_application
if settings.DEBUG and os.environ.get("IS_DEBUGPY_ATTACHED"):
import debugpy
debugpy.listen(("0.0.0.0", 9999))
# This allows easy placement of apps within the interior
# suite_backend directory.
BASE_DIR = Path(__file__).resolve(strict=True).parent.parent
sys.path.append(str(BASE_DIR / "suite_backend"))
# If DJANGO_SETTINGS_MODULE is unset, default to the local settings
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.local")
# This application object is used by any ASGI server configured to use this file.
django_application = get_asgi_application()
此外,我建议使用
python-dotenv包,而不是使用
os.environ
从 env 读取值,它负责解析布尔值并提供额外的功能。