Home > OS >  DJANGO WSGI: ModuleNotFound when deploying as monorepo
DJANGO WSGI: ModuleNotFound when deploying as monorepo

Time:07-31

I am trying to deploy a dockerized django app to digitalocean. It passes the build section, but shows the following error in deploying section: No module named 'backend-server'. 'backend-server' is the name of the directory that has all the backend files in my monorepo.

From the error log, I'm assuming there's a problem with my wsgi settings, but I cannot find where to start looking.

[2022-07-30 14:35:47] [2022-07-30 14:35:47  0000] [2] [INFO] Starting gunicorn 20.1.0
[2022-07-30 14:35:47] [2022-07-30 14:35:47  0000] [2] [INFO] Listening at: http://127.0.0.1:8000 (2)
[2022-07-30 14:35:47] [2022-07-30 14:35:47  0000] [2] [INFO] Using worker: sync
[2022-07-30 14:35:47] [2022-07-30 14:35:47  0000] [4] [INFO] Booting worker with pid: 4
[2022-07-30 14:35:47] [2022-07-30 14:35:47  0000] [4] [ERROR] Exception in worker process
[2022-07-30 14:35:47] Traceback (most recent call last):
[2022-07-30 14:35:47]   File "/usr/local/lib/python3.8/site-packages/gunicorn/arbiter.py", line 589, in spawn_worker
[2022-07-30 14:35:47]     worker.init_process()
[2022-07-30 14:35:47]   File "/usr/local/lib/python3.8/site-packages/gunicorn/workers/base.py", line 134, in init_process
[2022-07-30 14:35:47]     self.load_wsgi()
[2022-07-30 14:35:47]   File "/usr/local/lib/python3.8/site-packages/gunicorn/workers/base.py", line 146, in load_wsgi
[2022-07-30 14:35:47]     self.wsgi = self.app.wsgi()
[2022-07-30 14:35:47]   File "/usr/local/lib/python3.8/site-packages/gunicorn/app/base.py", line 67, in wsgi
[2022-07-30 14:35:47]     self.callable = self.load()
[2022-07-30 14:35:47]   File "/usr/local/lib/python3.8/site-packages/gunicorn/app/wsgiapp.py", line 58, in load
[2022-07-30 14:35:47]     return self.load_wsgiapp()
[2022-07-30 14:35:47]   File "/usr/local/lib/python3.8/site-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp
[2022-07-30 14:35:47]     return util.import_app(self.app_uri)
[2022-07-30 14:35:47]   File "/usr/local/lib/python3.8/site-packages/gunicorn/util.py", line 359, in import_app
[2022-07-30 14:35:47]     mod = importlib.import_module(module)
[2022-07-30 14:35:47]   File "/usr/local/lib/python3.8/importlib/__init__.py", line 127, in import_module
[2022-07-30 14:35:47]     return _bootstrap._gcd_import(name[level:], package, level)
[2022-07-30 14:35:47]   File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
[2022-07-30 14:35:47]   File "<frozen importlib._bootstrap>", line 991, in _find_and_load
[2022-07-30 14:35:47]   File "<frozen importlib._bootstrap>", line 961, in _find_and_load_unlocked
[2022-07-30 14:35:47]   File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
[2022-07-30 14:35:47]   File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
[2022-07-30 14:35:47]   File "<frozen importlib._bootstrap>", line 991, in _find_and_load
[2022-07-30 14:35:47]   File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked
[2022-07-30 14:35:47] ModuleNotFoundError: No module named 'backend-server'

wsgi.py

import os

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config")
os.environ.setdefault("DJANGO_CONFIGURATION", "Production")

from configurations.wsgi import get_wsgi_application  # noqa

application = get_wsgi_application()

DockerFile

FROM python:3.8
ENV PYTHONUNBUFFERED 1

# Allows docker to cache installed dependencies between builds
COPY ./requirements.txt requirements.txt
RUN pip install -r requirements.txt

# Adds our application code to the image
WORKDIR /code

EXPOSE 8000

# Run the production server
CMD newrelic-admin run-program gunicorn --bind localhost:$PORT --access-logfile - backend-server.wsgi:application

directory tree

monorepo/backend-server
├── Dockerfile
├── README.md
├── config
│   ├── common.py
│   ├── local.py
│   └── production.py
├── docker-compose.yml
├── manage.py
├── requirements.txt
├── urls.py
├── wait_for_postgres.py
└── wsgi.py

CodePudding user response:

Solved this issue by:

  1. Moving DockerFile and requirements.txt to the parent directory
monorepo/
├── Dockerfile
├── docker-compose.yml
├── requirements.txt
├── backend-server
  1. changing the dockerfile to the following

FROM python:3.8
ENV PYTHONUNBUFFERED 1

# Allows docker to cache installed dependencies between builds
COPY ./requirements.txt requirements.txt
RUN pip install -r requirements.txt

# Adds our application code to the image
WORKDIR /code/backend-server  
COPY backend-server .  
WORKDIR /code 

EXPOSE 8000

# Run the production server
CMD newrelic-admin run-program gunicorn --bind localhost:$PORT --access-logfile - backend-server.wsgi:application

CodePudding user response:

pip install backend

install this and may this solve this issue.

  • Related