'Dash Application works locally but not on Heroku

I have the following application which works locally on my pc but doesn't work on heroku. I get an application error

from dash import html
from dash import dcc
from datetime import date

df = pd.DataFrame()
app = dash.Dash(external_stylesheets=[dbc.themes.SPACELAB])
server = app.server

# In[2]:

# DECIDING THE APP LAYOUT
app.layout = html.Div(
    [
        # TITLE

And so on.

In my git ignore I have added venv *.pyc .env .DS_Store and my Procfile for the project has web:gunicorn main.py:server, with main.py being the python file containing the application.

I have followed the instructions after logging into Heroku in my Pycharm.

When I look at the logs I get the following error

2021-10-25T05:55:13.306092+00:00 app[web.1]:
2021-10-25T05:55:13.306092+00:00 app[web.1]: During handling of the above exception, another exception occurred:
2021-10-25T05:55:13.306092+00:00 app[web.1]:
2021-10-25T05:55:13.306099+00:00 app[web.1]: Traceback (most recent call last):
2021-10-25T05:55:13.306108+00:00 app[web.1]: File "/app/.heroku/python/bin/gunicorn", line 8, in <module>
2021-10-25T05:55:13.306173+00:00 app[web.1]: sys.exit(run())
2021-10-25T05:55:13.306182+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/app/wsgiapp.py", line 67, in run
2021-10-25T05:55:13.306250+00:00 app[web.1]: WSGIApplication("%(prog)s [OPTIONS] [APP_MODULE]").run()
2021-10-25T05:55:13.306258+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/app/base.py", line 231, in run
2021-10-25T05:55:13.306346+00:00 app[web.1]: super().run()
2021-10-25T05:55:13.306353+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/app/base.py", line 72, in run
2021-10-25T05:55:13.306418+00:00 app[web.1]: Arbiter(self).run()
2021-10-25T05:55:13.306425+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/arbiter.py", line 229, in run
2021-10-25T05:55:13.306775+00:00 app[web.1]: self.halt(reason=inst.reason, exit_status=inst.exit_status)
2021-10-25T05:55:13.306777+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/arbiter.py", line 342, in halt
2021-10-25T05:55:13.306894+00:00 app[web.1]: self.stop()
2021-10-25T05:55:13.306902+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/arbiter.py", line 393, in stop
2021-10-25T05:55:13.307013+00:00 app[web.1]: time.sleep(0.1)
2021-10-25T05:55:13.307020+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/arbiter.py", line 242, in handle_chld
2021-10-25T05:55:13.307105+00:00 app[web.1]: self.reap_workers()
2021-10-25T05:55:13.307112+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/arbiter.py", line 525, in reap_workers
2021-10-25T05:55:13.307237+00:00 app[web.1]: raise HaltServer(reason, self.WORKER_BOOT_ERROR)
2021-10-25T05:55:13.307272+00:00 app[web.1]: gunicorn.errors.HaltServer: <HaltServer 'Worker failed to boot.' 3>
2021-10-25T05:55:13.498847+00:00 heroku[web.1]: Process exited with status 1
2021-10-25T05:55:13.581325+00:00 heroku[web.1]: State changed from up to crashed
2021-10-25T05:55:13.637377+00:00 heroku[web.1]: State changed from crashed to starting
2021-10-25T05:55:22.528478+00:00 heroku[web.1]: Starting process with command `gunicorn main.py:server`
2021-10-25T05:55:23.685199+00:00 app[web.1]: [2021-10-25 05:55:23 +0000] [4] [INFO] Starting gunicorn 20.1.0
2021-10-25T05:55:23.685531+00:00 app[web.1]: [2021-10-25 05:55:23 +0000] [4] [INFO] Listening at: http://0.0.0.0:21869 (4)
2021-10-25T05:55:23.685586+00:00 app[web.1]: [2021-10-25 05:55:23 +0000] [4] [INFO] Using worker: sync
2021-10-25T05:55:23.688236+00:00 app[web.1]: [2021-10-25 05:55:23 +0000] [9] [INFO] Booting worker with pid: 9
2021-10-25T05:55:23.692651+00:00 app[web.1]: [2021-10-25 05:55:23 +0000] [10] [INFO] Booting worker with pid: 10
2021-10-25T05:55:24.228114+00:00 heroku[web.1]: State changed from starting to up
2021-10-25T05:55:25.097951+00:00 app[web.1]: [2021-10-25 05:55:25 +0000] [9] [ERROR] Exception in worker process
2021-10-25T05:55:25.097976+00:00 app[web.1]: Traceback (most recent call last):
2021-10-25T05:55:25.097983+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/util.py", line 359, in import_app
2021-10-25T05:55:25.097983+00:00 app[web.1]: mod = importlib.import_module(module)
2021-10-25T05:55:25.097985+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/importlib/__init__.py", line 127, in import_module
2021-10-25T05:55:25.097985+00:00 app[web.1]: return _bootstrap._gcd_import(name[level:], package, level)
2021-10-25T05:55:25.097986+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
2021-10-25T05:55:25.097986+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
2021-10-25T05:55:25.097986+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 981, in _find_and_load_unlocked
2021-10-25T05:55:25.097987+00:00 app[web.1]: ModuleNotFoundError: No module named 'main.py'; 'main' is not a package
2021-10-25T05:55:25.097987+00:00 app[web.1]:
2021-10-25T05:55:25.097988+00:00 app[web.1]: During handling of the above exception, another exception occurred:
2021-10-25T05:55:25.097988+00:00 app[web.1]:
2021-10-25T05:55:25.097988+00:00 app[web.1]: Traceback (most recent call last):
2021-10-25T05:55:25.097988+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/arbiter.py", line 589, in spawn_worker
2021-10-25T05:55:25.097989+00:00 app[web.1]: worker.init_process()
2021-10-25T05:55:25.097989+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/workers/base.py", line 134, in init_process
2021-10-25T05:55:25.097990+00:00 app[web.1]: self.load_wsgi()
2021-10-25T05:55:25.097990+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/workers/base.py", line 146, in load_wsgi
2021-10-25T05:55:25.097990+00:00 app[web.1]: self.wsgi = self.app.wsgi()
2021-10-25T05:55:25.097990+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/app/base.py", line 67, in wsgi
2021-10-25T05:55:25.097991+00:00 app[web.1]: self.callable = self.load()
2021-10-25T05:55:25.097991+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/app/wsgiapp.py", line 58, in load
2021-10-25T05:55:25.097991+00:00 app[web.1]: return self.load_wsgiapp()
2021-10-25T05:55:25.097991+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp
2021-10-25T05:55:25.097992+00:00 app[web.1]: return util.import_app(self.app_uri)
2021-10-25T05:55:25.097992+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/util.py", line 363, in import_app
2021-10-25T05:55:25.097992+00:00 app[web.1]: raise ImportError(msg % (module.rsplit(".", 1)[0], obj))
2021-10-25T05:55:25.097993+00:00 app[web.1]: ImportError: Failed to find application, did you mean 'main:server'?
2021-10-25T05:55:25.098095+00:00 app[web.1]: [2021-10-25 05:55:25 +0000] [9] [INFO] Worker exiting (pid: 9)
2021-10-25T05:55:25.165674+00:00 app[web.1]: [2021-10-25 05:55:25 +0000] [10] [ERROR] Exception in worker process
2021-10-25T05:55:25.165675+00:00 app[web.1]: Traceback (most recent call last):
2021-10-25T05:55:25.165676+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/util.py", line 359, in import_app
2021-10-25T05:55:25.165677+00:00 app[web.1]: mod = importlib.import_module(module)
2021-10-25T05:55:25.165677+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/importlib/__init__.py", line 127, in import_module
2021-10-25T05:55:25.165678+00:00 app[web.1]: return _bootstrap._gcd_import(name[level:], package, level)
2021-10-25T05:55:25.165678+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
2021-10-25T05:55:25.165680+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
2021-10-25T05:55:25.165680+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 981, in _find_and_load_unlocked
2021-10-25T05:55:25.165681+00:00 app[web.1]: ModuleNotFoundError: No module named 'main.py'; 'main' is not a package
2021-10-25T05:55:25.165681+00:00 app[web.1]:
2021-10-25T05:55:25.165682+00:00 app[web.1]: During handling of the above exception, another exception occurred:
2021-10-25T05:55:25.165682+00:00 app[web.1]:
2021-10-25T05:55:25.165683+00:00 app[web.1]: Traceback (most recent call last):
2021-10-25T05:55:25.165683+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/arbiter.py", line 589, in spawn_worker
2021-10-25T05:55:25.165683+00:00 app[web.1]: worker.init_process()
2021-10-25T05:55:25.165684+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/workers/base.py", line 134, in init_process
2021-10-25T05:55:25.165684+00:00 app[web.1]: self.load_wsgi()
2021-10-25T05:55:25.165684+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/workers/base.py", line 146, in load_wsgi
2021-10-25T05:55:25.165685+00:00 app[web.1]: self.wsgi = self.app.wsgi()
2021-10-25T05:55:25.165685+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/app/base.py", line 67, in wsgi
2021-10-25T05:55:25.165685+00:00 app[web.1]: self.callable = self.load()
2021-10-25T05:55:25.165685+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/app/wsgiapp.py", line 58, in load
2021-10-25T05:55:25.165686+00:00 app[web.1]: return self.load_wsgiapp()
2021-10-25T05:55:25.165686+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp
2021-10-25T05:55:25.165686+00:00 app[web.1]: return util.import_app(self.app_uri)
2021-10-25T05:55:25.165686+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/gunicorn/util.py", line 363, in import_app
2021-10-25T05:55:25.165686+00:00 app[web.1]: raise ImportError(msg % (module.rsplit(".", 1)[0], obj))
2021-10-25T05:55:25.165687+00:00 app[web.1]: ImportError: Failed to find application, did you mean 'main:server'?
2021-10-25T05:55:25.165869+00:00 app[web.1]: [2021-10-25 05:55:25 +0000] [10] [INFO] Worker exiting (pid: 10)
2021-10-25T05:55:25.239380+00:00 heroku[router]: at=error code=H13 desc="Connection closed without response" method=GET path="/" host=technical-analysis-v1.herokuapp.com request_id=9b5
7a9be-5fec-45ab-b1af-437fff3346e5 fwd="115.243.147.203" dyno=web.1 connect=5000ms service=353ms status=503 bytes=0 protocol=https
2021-10-25T05:55:25.240126+00:00 app[web.1]: [2021-10-25 05:55:25 +0000] [4] [WARNING] Worker with pid 10 was terminated due to signal 15
2021-10-25T05:55:25.335199+00:00 app[web.1]: [2021-10-25 05:55:25 +0000] [4] [INFO] Shutting down: Master
2021-10-25T05:55:25.335309+00:00 app[web.1]: [2021-10-25 05:55:25 +0000] [4] [INFO] Reason: Worker failed to boot.
2021-10-25T05:55:25.515903+00:00 heroku[web.1]: Process exited with status 3
2021-10-25T05:55:25.571178+00:00 heroku[web.1]: State changed from up to crashed
2021-10-25T05:55:30.750444+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=technical-analysis-v1.herokuapp.com request_id=70c23d17-fcff-4
7d1-81f9-d21a925f4b86 fwd="115.243.147.203" dyno=web.1 connect=5000ms service= status=503 bytes= protocol=https

Is there any way I can solve this error?



Solution 1:[1]

Try to change the Procfile with

web:gunicorn main:server

Indeed, the Procfile specifies the commands that are executed by the app on startup.

The main represents the name of the python file that runs your application or the name of the module it is in. 'Server' represents the app name that is named in your .py file.

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 Vicastor59130