Как заставить мой модуль wsgi распознаваться при запуске Python через docker?

Я использую Django 3.2 и Python 3.9. У меня такая структура каталогов

- docker-compose.yml
+ web
    - Dockerfile
    - entrypoint.sh
    - manage.py
    - requirements.txt
    + venv
        - ...
    + directory
        - settings.py
        - wsgi.py
        

Мой файл wsgi.py выглядит следующим образом

import time
import traceback
import signal
import sys
import pathlib
from django.core.handlers.wsgi import WSGIHandler
import os

# add the hellodjango project path into the sys.path
sys.path.append(pathlib.Path(__file__).parent.parent.parent.absolute())

# poiting to the project settings
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "directory.settings")

from django.core.wsgi import get_wsgi_application

try:
    application = get_wsgi_application()
except Exception:
    # Error loading applications
    if 'mod_wsgi' in sys.modules:
        traceback.print_exc()
        os.kill(os.getpid(), signal.SIGINT)
        time.sleep(2.5)

Мой раздел docker-compose.yml Python выглядит следующим образом

  web:
    restart: always
    build: ./web
    ports:           # to access the container from outside
      - "8000:8000"
    env_file: .env
    environment:
      DEBUG: 'true'
    volumes:
    - ./web/:/app
    depends_on:
      - postgres

Сценарий "entrypoint.sh" выглядит как

#!/bin/bash
set -e

cd /app
python manage.py migrate
python manage.py migrate directory
python manage.py insert_seed_data

python manage.py runserver

но когда я запускаю "docker-compose up", я получаю эту ошибку, жалуясь на то, что не могу найти модуль wsgi

web_1       | Exception in thread django-main-thread:
web_1       | Traceback (most recent call last):
web_1       |   File "/usr/local/lib/python3.9/site-packages/django/utils/module_loading.py", line 20, in import_string
web_1       |     return getattr(module, class_name)
web_1       | AttributeError: module 'directory.wsgi' has no attribute 'application'
web_1       | 
web_1       | The above exception was the direct cause of the following exception:
web_1       | 
web_1       | Traceback (most recent call last):
web_1       |   File "/usr/local/lib/python3.9/site-packages/django/core/servers/basehttp.py", line 45, in get_internal_wsgi_application
web_1       |     return import_string(app_path)
web_1       |   File "/usr/local/lib/python3.9/site-packages/django/utils/module_loading.py", line 22, in import_string
web_1       |     raise ImportError('Module "%s" does not define a "%s" attribute/class' % (
web_1       | ImportError: Module "directory.wsgi" does not define a "application" attribute/class
web_1       | 
web_1       | The above exception was the direct cause of the following exception:
web_1       | 
web_1       | Traceback (most recent call last):
web_1       |   File "/usr/local/lib/python3.9/threading.py", line 973, in _bootstrap_inner
web_1       |     self.run()
web_1       |   File "/usr/local/lib/python3.9/threading.py", line 910, in run
web_1       |     self._target(*self._args, **self._kwargs)
web_1       |   File "/usr/local/lib/python3.9/site-packages/django/utils/autoreload.py", line 64, in wrapper
web_1       |     fn(*args, **kwargs)
web_1       |   File "/usr/local/lib/python3.9/site-packages/django/core/management/commands/runserver.py", line 138, in inner_run
web_1       |     handler = self.get_handler(*args, **options)
web_1       |   File "/usr/local/lib/python3.9/site-packages/django/contrib/staticfiles/management/commands/runserver.py", line 27, in get_handler
web_1       |     handler = super().get_handler(*args, **options)
web_1       |   File "/usr/local/lib/python3.9/site-packages/django/core/management/commands/runserver.py", line 65, in get_handler
web_1       |     return get_internal_wsgi_application()
web_1       |   File "/usr/local/lib/python3.9/site-packages/django/core/servers/basehttp.py", line 47, in get_internal_wsgi_application
web_1       |     raise ImproperlyConfigured(
web_1       | django.core.exceptions.ImproperlyConfigured: WSGI application 'directory.wsgi.application' could not be loaded; Error importing module.

Не уверен, что мне нужно настроить, чтобы правильно импортировать этот модуль.

Вернуться на верх