【问题标题】:FLASK : plotly : Error: module 'index' has no attribute 'app.server'烧瓶:情节:错误:模块“索引”没有属性“app.server”
【发布时间】:2019-11-01 13:01:05
【问题描述】:

上下文

我正在使用 Visual Studio Code (VSCode) 调试应用程序。

没有击中断点!

  • 当我使用 launch.json 时没有命中断点(参见 [1])

  • 我可以使用这个 launch.json 进行调试(参见 [2]),但调试器不会在断点处停止!

我希望 VSCode 在必要时停止我的断点

**launch.json 下断点的正确配置是什么? **

感谢您投入时间帮助我!

项目的层次结构

  • launch.json
  • index.py 见 [4]
  • app.py 见 [3]
    • index.py
    • transactions.py
  • launch.json 如下所述 [1]

问题:错误:模块 'index' 没有属性 'app.server'

点击“开始调试> F5”后显示错误消息= 错误:模块'index'没有属性'app.server'

我尝试了几十种方法来设置 "FLASK_APP": "index:app.server",但它们会生成不同的错误消息:

  • "FLASK_APP": "index:app.server" 生成此错误 错误:未从 "index:app" 获得有效的 Flask 应用程序。

  • “FLASK_APP”:“index.py” 生成此错误 错误:无法在模块“index”中找到 Flask 应用程序或工厂。使用 "FLASK_APP=index:name 指定一个。

信息:gunicorn 命令(工作)

这是azure-pipelines.yml 中运行plotly 应用程序的命令:

  • gunicorn --bind=0.0.0.0 --timeout 600 index:app.server

附加文件

[1] launch.json - 不工作

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Flask",
            "type": "python",
            "request": "launch",
            "module": "flask",
            "env": {
                "FLASK_APP": "index:app.server",
                "FLASK_ENV": "development",
                "FLASK_DEBUG": "1",
                "FLASK_RUN_PORT": "8052"
            },
            "args": [
                "run",
                "--no-debugger",
                "--no-reload"
            ],
            "jinja": true
        }
    ]
}

[2] launch.json - 工作但没有命中断点

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${workspaceRoot}\\index.py",
            "console": "integratedTerminal"
        }
    ]
}

[3] webapp.py

​​>
# -*- coding: utf-8 -*-
import dash

app = dash.Dash(
    __name__, meta_tags=[{"name": "viewport",
                          "content": "width=device-width, initial-scale=1"}]
)
server = app.server
app.config.suppress_callback_exceptions = True

index.py - 应用的根目录

# -*- coding: utf-8 -*-
import dash_html_components as html
import dash_core_components as dcc
from webapp import app
from dash.dependencies import Input, Output
from pages import (
    transactions, index)


# Describe the layout/ UI of the app
app.layout = html.Div([
    dcc.Location(id="url", refresh=False),
    html.Div(id="page-content")
])


# Update page
@app.callback(Output("page-content", "children"),
              [Input("url", "pathname")])
def display_page(pathname):
    if pathname == "/dash/index":
        return index.layout
    if pathname == "/dash/transactions":
        return transactions.layout
    else:
        return index.layout


if __name__ == "__main__":
    app.run_server(debug=True, port=8051)

【问题讨论】:

    标签: python visual-studio-code plotly-dash vscode-debugger


    【解决方案1】:

    您的 [1] 示例不起作用,因为您将 FLASK_APP 设置为 index:app.server,它试图在 index 模块上查找名为 app.server 的属性。属性名称不能有点(您可以通过导入该模块并尝试getattr(index, "app.server") 来验证这一点)。你应该可以让FLASK_APP 简单地说index 让它工作。

    有关详细信息,请参阅Flask documentation on app discovery

    【讨论】:

      猜你喜欢
      • 2021-02-05
      • 1970-01-01
      • 2019-02-10
      • 2015-05-07
      • 2020-08-23
      • 2019-08-14
      • 1970-01-01
      • 1970-01-01
      • 2020-10-26
      相关资源
      最近更新 更多