在Windows上的visual studio代码中调试flask应用程序

时间:2017-11-28 14:40:30

标签: python flask visual-studio-code

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

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

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

我正在使用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

1 个答案:

答案 0 :(得分:0)

{
"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”,因为它对我不起作用