Debug a Flask (Python) web application in Visual Studio Code

后端 未结 8 1922
失恋的感觉
失恋的感觉 2020-12-11 04:28

How do I configure Visual Studio Code to debug a Flask (Python) web application?

For example, when I set the debugger in the view function, it should allow me to step

8条回答
  •  南笙
    南笙 (楼主)
    2020-12-11 05:13

    If you're using venv, make sure you have your virtual environment configured in Visual Studio Code.

    For general debugging features such as inspecting variables, setting breakpoints, and other activities that aren't language-dependent, review Visual Studio Code debugging.

    Your launch.json file should look something like this:

    {
      "version": "0.2.0",
      "configurations": [
        {
          "name": "Flask",
          "type": "python",
          "request": "launch",
          "stopOnEntry": false,
          "pythonPath": "${config:python.pythonPath}",
          "module": "flask.cli",
          "cwd": "${workspaceRoot}",
          "env": {
            "FLASK_APP": "sample",
            "LC_ALL": "en_US.utf-8",
            "LANG": "en_US.utf-8"
          },
          "args": [
            "run",
            "--no-debugger",
            "--no-reload"
          ],
          "envFile": "${workspaceRoot}/.env",
          "debugOptions": [
            "WaitOnAbnormalExit",
            "WaitOnNormalExit",
            "RedirectOutput"
          ]
        }
      ]
    }
    

    Set the FLASK_APP environment variable to the name of your application file.

    Set --no-debugger to avoid any potential conflicts with the Werkzeug debugger.

提交回复
热议问题