【问题标题】:Django app works on runserver, but fails on uwsgiDjango 应用程序在 runserver 上工作,但在 uwsgi 上失败
【发布时间】:2023-03-24 21:45:01
【问题描述】:

python3 manage.py runserver 工作正常,但在 nginx/uwsgi Django 应用程序上运行不起作用。 Uwsgi 日志:

binary reloading uWSGI...
chdir() to /
closing all non-uwsgi socket fds > 2 (max_fd = 1024)...
found fd 3 mapped to socket 0 (/home/nginx/pythonworld.sock)
running /usr/local/bin/uwsgi
[uWSGI] getting INI configuration from /home/dmitriym/new_pythonworld/pythonworld_wsgi.ini
*** Starting uWSGI 2.0.13.1 (64bit) on [Wed Jul 27 15:56:46 2016] ***
compiled with version: 4.8.4 on 27 July 2016 18:42:57
os: Linux-3.13.0-57-generic #95-Ubuntu SMP Fri Jun 19 09:28:15 UTC 2015
nodename: pythonworld.ru
machine: x86_64
clock source: unix
pcre jit disabled
detected number of CPU cores: 1
current working directory: /
detected binary path: /usr/local/bin/uwsgi
chdir() to /home/dmitriym/new_pythonworld/
your processes number limit is 7782
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 inherited UNIX address /home/nginx/pythonworld.sock fd 3
Python version: 3.4.3 (default, Oct 14 2015, 20:31:36)  [GCC 4.8.4]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x14ac400
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 145536 bytes (142 KB) for 1 cores
*** Operational MODE: single process ***
Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/django/apps/config.py", line 107, in create
    entry = module.default_app_config
AttributeError: 'module' object has no attribute 'default_app_config'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "./new_pythonworld/wsgi.py", line 16, in <module>
    application = get_wsgi_application()
  File "/usr/local/lib/python3.4/dist-packages/django/core/wsgi.py", line 13, in get_wsgi_application
    django.setup()
  File "/usr/local/lib/python3.4/dist-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/usr/local/lib/python3.4/dist-packages/django/apps/registry.py", line 85, in populate
    app_config = AppConfig.create(entry)
  File "/usr/local/lib/python3.4/dist-packages/django/apps/config.py", line 110, in create
    return cls(entry, module)
  File "/usr/local/lib/python3.4/dist-packages/django/apps/config.py", line 40, in __init__
    self.path = self._path_from_module(app_module)
  File "/usr/local/lib/python3.4/dist-packages/django/apps/config.py", line 73, in _path_from_module
    "with a 'path' class attribute." % (module, paths))
django.core.exceptions.ImproperlyConfigured: The app module <module 'bookshop' (namespace)> has multiple filesystem locations (['./bookshop', '/home/dmitriym/new_pythonworld/bookshop']); you must configure this app with an AppConfig subclass with a 'path' class attribute.
unable to load app 0 (mountpoint='') (callable not found or import error)
*** no app loaded. going in full dynamic mode ***
*** uWSGI is running in multiple interpreter mode ***
gracefully (RE)spawned uWSGI master process (pid: 1329)
spawned uWSGI worker 1 (pid: 1535, cores: 1)

我实际上在应用程序目录中有 init.py 文件。 并且在设置了 apps.py 和 init.py

之后
# apps.py
from django.apps import AppConfig

class MistypesConfig(AppConfig):
    name = 'bookshop'
    path = '/home/dmitriym/new_pythonworld/bookshop'

    def ready(self):
        pass


# __init__.py
default_app_config = 'bookshop.apps.MistypesConfig'

这个问题还是会出现。

项目的settings.py

INSTALLED_APPS = (
    'mistypes',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'vacancy',
    'bookshop',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',

    'django.middleware.gzip.GZipMiddleware',

    'django.middleware.common.CommonMiddleware',

    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.security.SecurityMiddleware',
)

ROOT_URLCONF = 'new_pythonworld.urls'
APPEND_SLASH = False

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                #'django.template.context_processors.debug',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]


WSGI_APPLICATION = 'new_pythonworld.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

pythonworld_wsgi.ini

[uwsgi]
# Django-related settings
# the base directory (full path)
chdir           = /home/dmitriym/new_pythonworld
# Djangos wsgi file
module          = new_pythonworld.wsgi
# process-related settings
# master
master          = true
# maximum number of worker processes
processes       = 1
#enable-threads  = true
#threads         = 4
# the socket (use the full path to be safe
socket          = /home/nginx/pythonworld.sock
# ... with appropriate permissions - may be needed
chmod-socket    = 664
# clear environment on exit
vacuum          = true
# pid file
pidfile         = /var/run/pythonworld.pid
# logging
logto           = /var/log/uwsgi/pythonworld.log
#stats           = /tmp/statsock
# uid and gid
uid             = nginx
gid             = www-data

还有 new_pythonworld/wsgi.py

"""
WSGI config for new_pythonworld project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/1.8/howto/deployment/wsgi/
"""

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "new_pythonworld.settings")

application = get_wsgi_application()

【问题讨论】:

  • new_pythonworld.wsgi.application 是什么? pythonworld_wsgi.ini 中有什么内容?
  • 添加了 new_pythonworld.wsgi.application 和 pythonworld_wsgi.ini

标签: python django python-3.x uwsgi


【解决方案1】:

通过更改允许 uwsgi 用户读取源代码的权限解决了问题。

【讨论】:

    【解决方案2】:

    我遇到了同样的错误,只需在所有应用中添加一个空的__init__.py 文件即可。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-18
      • 1970-01-01
      • 1970-01-01
      • 2014-08-18
      • 1970-01-01
      • 2011-04-11
      • 2021-03-05
      • 2012-04-13
      相关资源
      最近更新 更多