'VSCode dash app.run_server(debug=True) not working - runs but says 'no module named <file name>' and does not load

debug=True is not working in VSCode. When I try to run it, I get this error:

> Dash is running on http://127.0.0.1:8050/
> 
>  * Serving Flask app 'Test' (lazy loading)  * Environment: production 
> WARNING: This is a development server. Do not use it in a production
> deployment.    Use a production WSGI server instead.  * Debug mode: on
> No module named Test

Test is the name of the file - if I change it to something else, like app.py or main.py, it just says that instead.

The code itself is super simple and taken directly from the Plotly Dash tutorial site, https://dash.plotly.com/layout:

from dash import Dash, html, dcc 
import plotly.express as px
import pandas as pd

app = Dash(__name__)

# assume you have a "long-form" data frame
# see https://plotly.com/python/px-arguments/ for more options
df = pd.DataFrame({
    "Fruit": ["Apples", "Oranges", "Bananas", "Apples", "Oranges", "Bananas"],
    "Amount": [4, 1, 2, 2, 4, 5],
    "City": ["SF", "SF", "SF", "Montreal", "Montreal", "Montreal"] })

fig = px.bar(df, x="Fruit", y="Amount", color="City", barmode="group")

app.layout = html.Div(children=[
    html.H1(children='Hello Dash'),

    html.Div(children='''
        Dash: A web application framework for your data.
    '''),

    dcc.Graph(
        id='example-graph',
        figure=fig
    ) ])

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

If I turn debug off, it does open up the dashboard in a new browser window. Debug=True has also worked for me in the past (well, the debugging hasn't, but the dashboard has opened up in the browser when I reload), the only thing that changed is that I'm now using Azure DevOps as a repo, but I can't think why that would make a difference?

Any ideas?



Solution 1:[1]

When opening the VSCode editor, open the folder where the test.py file is. In case you need to use a module from a parent folder I use:

import sys 
sys.path.append('path_to_parent_folder')

Then run the debugger.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 S.B