启动gunicorn时如何解决'ModuleNotFoundError:No module named'mainpage''问题?

时间:2019-08-08 14:27:42

标签: django docker gunicorn

好吧,我正在尝试启动$ sudo docker-compose -f prod.yml up,并在Gunicorn启动gis工人时收到错误ModuleNotFoundError: No module named 'mainpage'

我的wsgi.py的路径正确,这是我的最后一个错误:D,我已经修复了它。

好吧,我将wsgi路径设置正确。

我尝试通过manage.py runserver运行它,并且效果很好。 我试图通过docker外部的manage.py runserver运行它,并且运行良好。

这是我的项目结构:

IRM
├── app
│   ├── backend
│   │   ├── api
│   │   ├── combinator
│   │   ├── crowler
│   │   ├── IRMback
│   │   │        ├── __init__.py
│   │   │        ├── settings.py
│   │   │        ├── urls.py
│   │   │        ├── views.py
│   │   │        ├── wsgi.py
│   │   ├── mainpage
│   │   ├── manage.py
│   │   └── ...
│   ├── frontend
│   │     └── ...
│   requirements
│   ├── base.txt
│   ├── local.txt
│   └── prod.txt
│   docker
│   ├── local
│             └── Dockerfile
│   ├── prod
│              └── Dockerfile
├── docker-compose.yml
└── prod.yml

这是我的prod.yml

version: '3'
volumes:
  pgdata:
services: 
  web:
    build:
      context: .
      dockerfile: docker/prod/python/Dockerfile
    volumes:
        - ./app:/app
    ports: 
      - "8000:8000"
    command: gunicorn -w 4 IRMback.IRMback.wsgi.application
      - postgres
  postgres:
    image: postgres:10
    environment: 
      POSTGRES_USER: postgres
      POSTGRES_DB: postgres
      POSTGRES_PASS: admin
    volumes:
      - pgdata:/var/lib/postgresql/data
    ports:
      - 5433:5433

这是我的Dockerfile

FROM python:buster

ENV PYTHONUNBUFFERED 1

COPY app /app

WORKDIR /app

RUN pip install -r requirements/prod.txt

至少,这是我的settings.py的一部分,与我的问题有关:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',

    'rest_framework',
    'rest_framework.authtoken',
    'rest_auth',
    'allauth',
    'allauth.socialaccount',
    'allauth.socialaccount.providers.vk',
    'allauth.socialaccount.providers.google',
    'allauth.account',
    'rest_auth.registration',
    'corsheaders',

    'mainpage',
    'combinator'
]
WSGI_APPLICATION = 'IRMback.wsgi.application'

直到我将此路径写入wsgi(IRMback.IRMback.wsgi.application中的prod.yml),根据我尝试的路径,它是一个例外ModuleNotFoundError: No module named 'IRMback'``(or IRMback.IRMback)

除了运行没有c之外,我都没有。

我应该在所有应用程序中添加wsgi.py还是什么?我在哪里可以读到它? 在Google上长期搜索Exception文本并没有带来任何结果。

1 个答案:

答案 0 :(得分:2)

您的问题似乎是IRMbackbackend的混合,这有点令人困惑。

在Dockerfile中,尝试设置WORKDIR /app/backend。然后从gunicorn命令中删除一个IRMback

gunicorn -w 4 IRMback.wsgi.application

这应允许将位于/app/backend/mainpage的模块导入为mainpage