简体   繁体   English

在Windows上的Visual Studio代码中调试Flask应用

[英]Debug flask app in visual studio code on windows

I am trying to debug python flask app from VSCode using the VSCode-Python extension. 我正在尝试使用VSCode-Python扩展从VSCode调试python flask应用程序。 According two the documentation there are 2 ways to make it work: 根据两个文档,有两种方法可以使其起作用:

  • Use the "module":"flask.cli" option in launch.json 在launch.json中使用"module":"flask.cli"选项
  • Use a startup script, which imports flask.cli module. 使用启动脚本,该脚本导入flask.cli模块。 Described here 在这里描述

For both ways I end up with: OSError: Windows error 1 . 对于这两种方式,我最终都会OSError: Windows error 1 That seems to be an error indicating that the main function imported from flask.cli does not exists. 这似乎是一个错误,指示从flask.cli导入的main功能不存在。

I am using virtualenv. 我正在使用virtualenv。 If I try to run from command line, the application works fine. 如果我尝试从命令行运行,则该应用程序可以正常运行。

Here is the settings.json content (env is the folder which contains the environment scripts): 这是settings.json的内容(env是包含环境脚本的文件夹):

{
  "python.pythonPath": "${workspaceRoot}\\backend\\env\\Scripts\\python.exe"
}

And here is the launch.json: 这是launch.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Flask (0.11.x or later)",
            "type": "python",
            "request": "launch",
            "stopOnEntry": false,
            "pythonPath": "${config:python.pythonPath}",
            "cwd": "${workspaceRoot}",
            "module":"flask.cli",
            "env": {
                "FLASK_APP": "${workspaceRoot}\\backend\\app.py"
            },
            "args": [
                "run",
                "--no-debugger",
                "--no-reload"
            ],
            "debugOptions": [
                "WaitOnAbnormalExit",
                "WaitOnNormalExit",
                "RedirectOutput"
            ]
        }
    ]
}

Here is the complete error stack trace: 这是完整的错误堆栈跟踪:

runpy.py:125: RuntimeWarning: 'flask.cli' found in sys.modules after import of package 'flask', but prior to execution of 'flask.cli'; this may result in unpredictable behaviour
  warn(RuntimeWarning(msg))
line 205, in run_module return _run_module_code(code, init_globals, run_name, mod_spec)
line 96, in _run_module_code mod_name, mod_spec, pkg_name, script_name)
line 85, in _run_code exec(code, run_globals)
line 517, in <module> main(as_module=True)
line 513, in main cli.main(args=args, prog_name=name)
line 380, in main return AppGroup.main(self, *args, **kwargs)
line 707, in main e.show()
line 47, in show echo(self.ctx.get_usage() + '\n', file=file, color=color)
line 259, in echo file.write(message)
line 180, in write return self._text_stream.write(x)
line 164, in write raise OSError(self._get_error_message(GetLastError()))
OSError: Windows error 1
{
"version": "0.2.0",
"configurations": [
    {
        "name": "Flask app",
        "type": "python",
        "request": "launch",
        "stopOnEntry": false,
        "pythonPath": "${config:python.pythonPath}",
        "program": "${file}",
        "cwd": "${workspaceRoot}",
        "env": {
            "FLASK_APP": "${workspaceRoot}\\backend\\app.py"
        },
        "args": [
            "run",
            "--no-debugger",
            "--no-reload"
        ],
        "envFile": "${workspaceRoot}/.env",
        "debugOptions": [
            "WaitOnAbnormalExit",
            "WaitOnNormalExit",
            "RedirectOutput"
        ]
    }
],
"compounds": []
}

I think you should remove "module": "flask.cli" because it did not work for me 我认为您应该删除“ module”:“ flask.cli”,因为它对我不起作用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM