Hosting tutorial Dash app on Render: AttributeError: module 'main' has no attribute 'server'

During the weekend I spent 6-7 hours working on setting up a Dash page on Render following this tutorial:

It worked at first and I was able to make minor changes. At one point it broke down, and gave the error below. Even after changing the code again and again and recreating the version I hard working prior I can’t fix this error. If anyone can solve this it would be a great help.

I followed this tutorial: Deploy your Python Data App to the Web for Free - Dash - YouTube
The setup for the webservice on Render follows the instructions in the video

The error message I get is very long in the Render window, but it is this:

Aug 13 10:06:54 PM  Traceback (most recent call last):
Aug 13 10:06:54 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/gunicorn/util.py", line 414, in import_app
Aug 13 10:06:54 PM      app = getattr(mod, name)
Aug 13 10:06:54 PM  AttributeError: module 'main' has no attribute 'server'
Aug 13 10:06:54 PM  
Aug 13 10:06:54 PM  During handling of the above exception, another exception occurred:
Aug 13 10:06:54 PM  
Aug 13 10:06:54 PM  Traceback (most recent call last):
Aug 13 10:06:54 PM    File "/opt/render/project/src/.venv/bin/gunicorn", line 8, in <module>
Aug 13 10:06:54 PM      sys.exit(run())
Aug 13 10:06:54 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 67, in run
Aug 13 10:06:54 PM      WSGIApplication("%(prog)s [OPTIONS] [APP_MODULE]").run()
Aug 13 10:06:54 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/gunicorn/app/base.py", line 236, in run
Aug 13 10:06:54 PM      super().run()
Aug 13 10:06:54 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/gunicorn/app/base.py", line 72, in run
Aug 13 10:06:54 PM      Arbiter(self).run()
Aug 13 10:06:54 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/gunicorn/arbiter.py", line 58, in __init__
Aug 13 10:06:54 PM      self.setup(app)
Aug 13 10:06:54 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/gunicorn/arbiter.py", line 118, in setup
Aug 13 10:06:54 PM      self.app.wsgi()
Aug 13 10:06:54 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/gunicorn/app/base.py", line 67, in wsgi
Aug 13 10:06:54 PM      self.callable = self.load()
Aug 13 10:06:54 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 58, in load
Aug 13 10:06:54 PM      return self.load_wsgiapp()
Aug 13 10:06:54 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp
Aug 13 10:06:54 PM      return util.import_app(self.app_uri)
Aug 13 10:06:54 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/gunicorn/util.py", line 418, in import_app
Aug 13 10:06:54 PM      raise AppImportError("Failed to find attribute %r in %r." % (name, module))
Aug 13 10:06:54 PM  gunicorn.errors.AppImportError: Failed to find attribute 'server' in 'main'.
Aug 13 10:06:57 PM  ==> Using Node version 14.17.0 (default)
Aug 13 10:06:57 PM  ==> Docs on specifying a Node version: https://render.com/docs/node-version
Aug 13 10:06:59 PM  ==> Starting service with 'gunicorn main:server'
Aug 13 10:07:05 PM  Traceback (most recent call last):
Aug 13 10:07:05 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/gunicorn/util.py", line 414, in import_app
Aug 13 10:07:05 PM      app = getattr(mod, name)
Aug 13 10:07:05 PM  AttributeError: module 'main' has no attribute 'server'
Aug 13 10:07:05 PM  
Aug 13 10:07:05 PM  During handling of the above exception, another exception occurred:
Aug 13 10:07:05 PM  
Aug 13 10:07:05 PM  Traceback (most recent call last):
Aug 13 10:07:05 PM    File "/opt/render/project/src/.venv/bin/gunicorn", line 8, in <module>
Aug 13 10:07:05 PM      sys.exit(run())
Aug 13 10:07:05 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 67, in run
Aug 13 10:07:05 PM      WSGIApplication("%(prog)s [OPTIONS] [APP_MODULE]").run()
Aug 13 10:07:05 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/gunicorn/app/base.py", line 236, in run
Aug 13 10:07:05 PM      super().run()
Aug 13 10:07:05 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/gunicorn/app/base.py", line 72, in run
Aug 13 10:07:05 PM      Arbiter(self).run()
Aug 13 10:07:05 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/gunicorn/arbiter.py", line 58, in __init__
Aug 13 10:07:05 PM      self.setup(app)
Aug 13 10:07:05 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/gunicorn/arbiter.py", line 118, in setup
Aug 13 10:07:05 PM      self.app.wsgi()
Aug 13 10:07:05 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/gunicorn/app/base.py", line 67, in wsgi
Aug 13 10:07:05 PM      self.callable = self.load()
Aug 13 10:07:05 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 58, in load
Aug 13 10:07:05 PM      return self.load_wsgiapp()
Aug 13 10:07:05 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp
Aug 13 10:07:05 PM      return util.import_app(self.app_uri)
Aug 13 10:07:05 PM    File "/opt/render/project/src/.venv/lib/python3.7/site-packages/gunicorn/util.py", line 418, in import_app
Aug 13 10:07:05 PM      raise AppImportError("Failed to find attribute %r in %r." % (name, module))
Aug 13 10:07:05 PM  gunicorn.errors.AppImportError: Failed to find attribute 'server' in 'main'.

My current code is:

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

df_airports = pd.read_csv(
    "https://raw.githubusercontent.com/plotly/datasets/master/2011_february_us_airport_traffic.csv"
)

app = Dash(__name__)
#app.title("SR Test App")

server = app.server

app.layout = html.Div(
    [
        dcc.Graph(id="graph"),
        html.P(
            "Enter the type of graph you want to plot: (scatter_geo or scatter_mapbox)"
        ),
        dcc.Dropdown(
            id="type",
            options=["scatter_mapbox", "scatter_geo"],
            value="scatter_mapbox",
            clearable=False,
        ),
    ]
)


@app.callback(
    Output("graph", "figure"),
    Input("type", "value"),
)
    
def generate_chart(values):
    fig = px.scatter_mapbox(
        df_airports,
        lat="lat",
        lon="long",
        hover_data=["airport", "city", "state", "cnt"],
        size="cnt",
        color="cnt",
        zoom=3 )
    fig.update_layout(mapbox_style="open-street-map")
    
    # Set the size of the plot
    fig.update_layout(width=1000, height=3000)

    return fig


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

The error indicates a wrongly configured entrypoint. What’s the name of the file for which you posted the code? What command did you run on Render to start the app?

Hi Emil

The Python file is called main.py and on Render I just click ‘deploy latest commit’, so running the code from GitHub.

What is surprising to me is that the file name and the deploy method are both unchanged. I seem to do everything the exact same as when it was working.

I have now tried running it locally, and it works right away. So there is something wrong with the hosting on Render. The weird part is that I’m 99% sure I did not change the settings on Render, so I don’t see how it could work at first only to fail later. One odd possibility is that I ran out of hosting time on the free plan, but surely they would have told me so then.