I am trying to deploy my Flask app (to https://railway.app/).
My project structure looks like this:
myproject/server
├── Procfile
├── __init__.py
├── app.py
├── requirements.txt
├── sources
│ ├── __init__.py
│ ├── integrations
│ ├── source_controller.py
│ ├── sources.py
And in app.py
, I have the following imports:
from flask import Flask, request
from server.sources.source_controller import SourceController
from server.sources.sources import Source
While I can run my server just fine locally (with flask --app app run
), when I try calling gunicorn app:app
(which is what railway calls when deploying my app), I get the following error:
[2024-07-02 19:43:34 -0400] [65259] [ERROR] Exception in worker process
Traceback (most recent call last):
File "/usr/local/lib/python3.11/site-packages/gunicorn/arbiter.py", line 609, in spawn_worker
worker.init_process()
File "/usr/local/lib/python3.11/site-packages/gunicorn/workers/base.py", line 134, in init_process
self.load_wsgi()
File "/usr/local/lib/python3.11/site-packages/gunicorn/workers/base.py", line 146, in load_wsgi
self.wsgi = self.app.wsgi()
^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/gunicorn/app/base.py", line 67, in wsgi
self.callable = self.load()
^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/gunicorn/app/wsgiapp.py", line 58, in load
return self.load_wsgiapp()
^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp
return util.import_app(self.app_uri)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/gunicorn/util.py", line 371, in import_app
mod = importlib.import_module(module)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/Cellar/[email protected]/3.11.6_1/Frameworks/Python.framework/Versions/3.11/lib/python3.11/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 940, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "/Users/marcoeangeli/Projects/tomes/server/app.py", line 3, in <module>
from server.sources.source_controller import SourceController
ModuleNotFoundError: No module named 'server'
[2024-07-02 19:43:34 -0400] [65259] [INFO] Worker exiting (pid: 65259)
... other logs ...
I have tried searching for this error but I can’t find anything that helps. I pretty confused why my IDE is happy with the way I’m importing, running the app directly runs fine, but Gunicorn fails!