首页 > 解决方案 > ImportError:无法从“app”导入名称“app”

问题描述

我对 dash 还很陌生,我正在尝试遵循一些更高级的指南(链接:here),这些指南着眼于多页仪表板,但似乎在一路上遇到的每一个障碍都失败了。

我已经成功地创建了我的虚拟环境,安装了所需的包,并且我刚刚创建了“app.py”文件,但是当我创建并运行“index.py”文件时,我看到了以下错误:

ImportError: cannot import name 'app' from 'app'

我正在运行的 app.py 文件是使用以下代码创建的:

import dash
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
server = app.server
#app.config.suppress_callback_exceptions = True ####<-- tutorial said to run this line but as suppress was giving me errors I removed it

我正在运行的“index.py”文件如下所示:

from app import app
from app import server
import dash_html_components as html
import dash_core_components as dcc

app.layout = html.Div([html.H1('Dash Demo Graph',
                               style={
                                      'textAlign': 'center',
                                      "background": "yellow"}),
                       dcc.Graph(
                           id='graph-1',
                           figure={
                               'data': [
                                   {'x': [1, 2, 3, 4, 5, 6, 7], 'y': [10, 20, 30, 40, 50, 60, 70], 'type': 'line', 'name': 'value1'},
                                   {'x': [1, 2, 3, 4, 5, 6, 7], 'y': [12, 22, 36, 44, 49, 58, 73], 'type': 'line', 'name': 'value2'}
                               ],
                               'layout': {
                                   'title': 'Simple Line Graph',
                                     }
                                 }
                            ),

                            ], style={
                                "background": "#000080"}
                         )

if __name__ == '__main__':
    app.run_server(debug=True)

我在使用 Jupyter Lab 和 python 3.8 的 Windows 机器上,运行代码的目录包含以下文件夹和文件:

.ipynb_checkpoints
__pycache__
env
app.py
index.py

任何有关我如何解决此错误的帮助将不胜感激。

标签: pythonplotly-dash

解决方案


推荐阅读