【问题标题】:Debug flask app in visual studio code on windows在 Windows 上的 Visual Studio 代码中调试烧瓶应用程序
【发布时间】:2017-11-28 14:40:29
【问题描述】:

我正在尝试使用 VSCode-Python 扩展从 VSCode 调试 python flask 应用程序。根据两个文档,有两种方法可以使其工作:

  • 在 launch.json 中使用 "module":"flask.cli" 选项
  • 使用导入flask.cli 模块的启动脚本。描述here

对于这两种方式,我最终都会得到:OSError: Windows error 1。这似乎是一个错误,表明从flask.cli 导入的main 函数不存在。

我正在使用 virtualenv。如果我尝试从命令行运行,应用程序运行正常。

这是 settings.json 的内容(env 是包含环境脚本的文件夹):

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

这是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"
            ]
        }
    ]
}

这是完整的错误堆栈跟踪:

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

【问题讨论】:

    标签: python flask visual-studio-code


    【解决方案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": []
    }
    

    我认为你应该删除 "module": "flask.cli" 因为它对我不起作用

    【讨论】:

    • 我没有测试我们的方法。毕竟我已经设法让它与flask.cli一起工作,错误只是我指向了一个错误的python文件。错误信息让我很困惑
    猜你喜欢
    • 2018-06-20
    • 2018-07-27
    • 1970-01-01
    • 2016-10-28
    • 2014-08-21
    • 2020-08-19
    • 1970-01-01
    • 2018-12-10
    相关资源
    最近更新 更多