1

I'm trying to deploy a django app to heroku.

My directory structure is:

'root'/
  Procfile
  backend/
    django_app/
      manage.py
      mainapp/
        settings.py
        wsgi.py      

I tried to follow the indications given on some posts here on stackoverflow but to no avail.

I have a Procfile in the root directory. The latest version has:

web: gunicorn backend.django_app.mainapp.wsgi --log-file -

But it still gives me a module not found error:

2021-05-01T20:20:33.556737+00:00 heroku[web.1]: State changed from crashed to starting
2021-05-01T20:20:58.799412+00:00 heroku[web.1]: Starting process with command `gunicorn backend.django_app.mainapp.wsgi --log-file -`
2021-05-01T20:21:01.738041+00:00 app[web.1]: [2021-05-01 20:21:01 +0000] [4] [INFO] Starting gunicorn 20.0.4
2021-05-01T20:21:01.739021+00:00 app[web.1]: [2021-05-01 20:21:01 +0000] [4] [INFO] Listening at: http://0.0.0.0:46010 (4)
2021-05-01T20:21:01.739198+00:00 app[web.1]: [2021-05-01 20:21:01 +0000] [4] [INFO] Using worker: sync
2021-05-01T20:21:01.744319+00:00 app[web.1]: [2021-05-01 20:21:01 +0000] [8] [INFO] Booting worker with pid: 8
2021-05-01T20:21:02.007630+00:00 app[web.1]: [2021-05-01 20:21:02 +0000] [8] [ERROR] Exception in worker process
2021-05-01T20:21:02.007644+00:00 app[web.1]: Traceback (most recent call last):
2021-05-01T20:21:02.007645+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
2021-05-01T20:21:02.007645+00:00 app[web.1]: worker.init_process()
2021-05-01T20:21:02.007646+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/workers/base.py", line 119, in init_process
2021-05-01T20:21:02.007646+00:00 app[web.1]: self.load_wsgi()
2021-05-01T20:21:02.007647+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/workers/base.py", line 144, in load_wsgi
2021-05-01T20:21:02.007648+00:00 app[web.1]: self.wsgi = self.app.wsgi()
2021-05-01T20:21:02.007648+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/app/base.py", line 67, in wsgi
2021-05-01T20:21:02.007648+00:00 app[web.1]: self.callable = self.load()
2021-05-01T20:21:02.007649+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/app/wsgiapp.py", line 49, in load
2021-05-01T20:21:02.007649+00:00 app[web.1]: return self.load_wsgiapp()
2021-05-01T20:21:02.007649+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/app/wsgiapp.py", line 39, in load_wsgiapp
2021-05-01T20:21:02.007650+00:00 app[web.1]: return util.import_app(self.app_uri)
2021-05-01T20:21:02.007650+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/gunicorn/util.py", line 358, in import_app
2021-05-01T20:21:02.007650+00:00 app[web.1]: mod = importlib.import_module(module)
2021-05-01T20:21:02.007651+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/importlib/__init__.py", line 127, in import_module
2021-05-01T20:21:02.007651+00:00 app[web.1]: return _bootstrap._gcd_import(name[level:], package, level)
2021-05-01T20:21:02.007651+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
2021-05-01T20:21:02.007652+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 991, in _find_and_load
2021-05-01T20:21:02.007652+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
2021-05-01T20:21:02.007652+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
2021-05-01T20:21:02.007652+00:00 app[web.1]: File "<frozen importlib._bootstrap_external>", line 783, in exec_module
2021-05-01T20:21:02.007653+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
2021-05-01T20:21:02.007653+00:00 app[web.1]: File "/app/backend/django_app/mainapp/wsgi.py", line 16, in <module>
2021-05-01T20:21:02.007653+00:00 app[web.1]: application = get_wsgi_application()
2021-05-01T20:21:02.007654+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
2021-05-01T20:21:02.007654+00:00 app[web.1]: django.setup(set_prefix=False)
2021-05-01T20:21:02.007655+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/django/__init__.py", line 19, in setup
2021-05-01T20:21:02.007655+00:00 app[web.1]: configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
2021-05-01T20:21:02.007655+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/django/conf/__init__.py", line 83, in __getattr__
2021-05-01T20:21:02.007656+00:00 app[web.1]: self._setup(name)
2021-05-01T20:21:02.007656+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/django/conf/__init__.py", line 70, in _setup
2021-05-01T20:21:02.007656+00:00 app[web.1]: self._wrapped = Settings(settings_module)
2021-05-01T20:21:02.007656+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/site-packages/django/conf/__init__.py", line 177, in __init__
2021-05-01T20:21:02.007657+00:00 app[web.1]: mod = importlib.import_module(self.SETTINGS_MODULE)
2021-05-01T20:21:02.007657+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.8/importlib/__init__.py", line 127, in import_module
2021-05-01T20:21:02.007657+00:00 app[web.1]: return _bootstrap._gcd_import(name[level:], package, level)
2021-05-01T20:21:02.007658+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
2021-05-01T20:21:02.007658+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 991, in _find_and_load
2021-05-01T20:21:02.007658+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 961, in _find_and_load_unlocked
2021-05-01T20:21:02.007658+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
2021-05-01T20:21:02.007659+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
2021-05-01T20:21:02.007659+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 991, in _find_and_load
2021-05-01T20:21:02.007659+00:00 app[web.1]: File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked
2021-05-01T20:21:02.007660+00:00 app[web.1]: ModuleNotFoundError: No module named 'mainapp'
2021-05-01T20:21:02.007864+00:00 app[web.1]: [2021-05-01 20:21:02 +0000] [8] [INFO] Worker exiting (pid: 8)
2021-05-01T20:21:02.078511+00:00 app[web.1]: [2021-05-01 20:21:02 +0000] [4] [INFO] Shutting down: Master
2021-05-01T20:21:02.078591+00:00 app[web.1]: [2021-05-01 20:21:02 +0000] [4] [INFO] Reason: Worker failed to boot.
2021-05-01T20:21:02.152800+00:00 heroku[web.1]: Process exited with status 3
2021-05-01T20:21:02.289079+00:00 heroku[web.1]: State changed from starting to crashed
2021-05-01T20:21:10.000000+00:00 app[api]: Build succeeded

Any help would be appreciated.

Thanks oin advance.

mehrlicht
  • 75
  • 1
  • 11
  • Have you tried modifying your Procfile as shown [here](https://stackoverflow.com/questions/16416172/how-can-i-modify-procfile-to-run-gunicorn-process-in-a-non-standard-folder-on-he)? – Y.S. May 01 '21 at 21:08
  • yes. `gunicorn --pythonpath django_app mainapp.wsgi --log-file -` but the result is the same – mehrlicht May 01 '21 at 21:26
  • Does adding backend change anything? `gunicorn --pythonpath backend django_app mainapp.wsgi --log-file -` – Y.S. May 01 '21 at 21:50
  • Apparently it works with `gunicorn backend.django_app.mainapp.wsgi --log-file -`. But now I get another error. I'll edit the question. – mehrlicht May 02 '21 at 09:31

2 Answers2

0

The error message states: ModuleNotFoundError: No module named 'mainapp'. It is trying to use mainapp as a module. In order to do that, you will need to add __init__.py files in your directories.

'root'/
  Procfile
  backend/
    django_app/
      __init__.py
      manage.py
      mainapp/
        __init__.py
        settings.py
        wsgi.py      

You can read more about __init__.py files and its contents here.

pazitos10
  • 1,641
  • 16
  • 25
  • I do have the `__init__.py`s on each module. I just left it out (as well as all the models, views, urls, admin, apps, migrations/ – mehrlicht May 02 '21 at 09:09
  • Apparently it works with `gunicorn backend.django_app.mainapp.wsgi --log-file -`. But now I get another error. I'll edit the question. – mehrlicht May 02 '21 at 09:31
0

I finally solved it. Thanks for all the inputs.

The final version of the Procfile is like this:

web: gunicorn -b 0.0.0.0:8000 backend.django_app.mainapp:application --log-file -
mehrlicht
  • 75
  • 1
  • 11