【问题标题】:Unable to Import Django Application into uWSGI无法将 Django 应用程序导入 uWSGI
【发布时间】:2017-05-05 18:08:40
【问题描述】:

在我的 Google Compute Engine 实例上,我一直在尝试按照以下步骤设置我的应用服务器 (uWSGI):

  1. cd /path/to/project/directory
  2. 在 virtual_env 上工作
  3. uwsgi --ini uwsgi.ini

每次我初始化 uWSGI 时,都会抛出以下错误(下面是更广泛的日志):

ImportError: No module named '"adtor'
unable to load app 0 (mountpoint='') (callable not found or import error)

我能够单独运行开发服务器(python manage.py runserver)。奇怪的是,在我的 django 应用程序名称之前有一个额外的双引号 ("),但我无法在代码、.ini、虚拟 env 挂钩(例如 postactivate)中找到任何额外双引号的实例。除此之外,我'无法找到潜在的罪魁祸首;我假设它在我的 .ini 文件中或未正确安装某些第三方库。

“点冻结”:

appdirs==1.4.3
Django==1.8.5
django-filter==1.0.2
django-model-utils==3.0.0
djangorestframework==3.6.2
mysqlclient==1.3.10
packaging==16.8
pyparsing==2.2.0
six==1.10.0
uWSGI==2.0.15

uwsgi.ini

[uwsgi]
socket=127.0.0.1:8000
stats=127.0.0.1:9191
http-timeout=1800

uid=www-data
gid=www-data

virtualenv=/home/tor/.virtualenvs/adtor_prod
home=/home/tor/.virtualenvs/adtor_prod
chdir=/srv/django/adtor_project/prime
module=adtor.wsgi:application
master=true
vacuum=true
thunder-lock=true
pidfile=/tmp/project-master.pid
daemonize=/var/log/uwsgi/adtor-blog.log

threads=2
enable-threads=true

for-readline = /etc/srv/vars.txt
  env = %(_)
endfor =

日志:

*** Starting uWSGI 2.0.15 (64bit) on [Fri May  5 16:00:56 2017] ***
compiled with version: 4.8.4 on 02 May 2017 17:47:17
os: Linux-4.4.0-75-generic #96~14.04.1-Ubuntu SMP Thu Apr 20 11:06:30 UTC 2017
nodename: instance-1
machine: x86_64
clock source: unix
detected number of CPU cores: 1
current working directory: /srv/django/adtor_project
writing pidfile to /tmp/project-master.pid
detected binary path: /home/tor/.virtualenvs/adtor_prod/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
chdir() to /srv/django/adtor_project/prime
your processes number limit is 2257
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: enabled
uwsgi socket 0 bound to TCP address 127.0.0.1:8000 fd 3
Python version: 3.4.3 (default, Nov 17 2016, 01:12:14)  [GCC 4.8.4]
Set PythonHome to /home/tor/.virtualenvs/adtor_prod
Python main interpreter initialized at 0x1a02ab0
python threads support enabled
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 166112 bytes (162 KB) for 2 cores
*** Operational MODE: threaded ***
Traceback (most recent call last):
  File "./adtor/wsgi.py", line 16, in <module>
    application = get_wsgi_application()
  File "/home/tor/.virtualenvs/adtor_prod/lib/python3.4/site-packages/django/core/wsgi.py", line 14, in get_wsgi_application
    django.setup()
  File "/home/tor/.virtualenvs/adtor_prod/lib/python3.4/site-packages/django/__init__.py", line 17, in setup
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
  File "/home/tor/.virtualenvs/adtor_prod/lib/python3.4/site-packages/django/conf/__init__.py", line 48, in __getattr__
    self._setup(name)
  File "/home/tor/.virtualenvs/adtor_prod/lib/python3.4/site-packages/django/conf/__init__.py", line 44, in _setup
    self._wrapped = Settings(settings_module)
  File "/home/tor/.virtualenvs/adtor_prod/lib/python3.4/site-packages/django/conf/__init__.py", line 92, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/home/tor/.virtualenvs/adtor_prod/lib/python3.4/importlib/__init__.py", line 109, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
ImportError: No module named '"adtor'
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 ***
spawned uWSGI master process (pid: 8753)
spawned uWSGI worker 1 (pid: 8755, cores: 2)
*** Stats server enabled on 127.0.0.1:9191 fd: 10 ***

【问题讨论】:

  • 这个额外的"故事看起来像The double quotes were not proper double quotes,即而不是"。即使显然不是,因为您的回溯确实显示了真正的双引号。
  • 看来我正确使用了双引号。我把它改成单引号,收到错误:ImportError: No module named "'adtor" 好像多余的单/双引号与它无关
  • 你的os.environ.setdefault("DJANGO_SETTINGS_MODULE", "adtor.settings")是否也正确地写在了wsgi.py中?
  • 是的。我一直在使用默认的 wsgi.py 文件,如中,对该文件进行了零更改
  • 您曾经能够运行您的生产站点吗?

标签: django google-compute-engine uwsgi


【解决方案1】:

这可能不是答案,但是:请注意,如果您的路径是文件还是目录,没有尾部斜杠会导致不清楚。话虽如此,让我们尝试考虑一下您的目录:

[uwsgi]
...
virtualenv=/home/tor/.virtualenvs/adtor_prod/
home=/home/tor/.virtualenvs/adtor_prod/
chdir=/srv/django/adtor_project/prime/
...

您也可以尝试在module=adtor.wsgi:application下方添加这一行

...
module=adtor.wsgi:application
env DJANGO_SETTINGS_MODULE=adtor.settings
...

【讨论】:

    【解决方案2】:

    在我的 uwsgi.ini 文件中,我从另一个文件错误地加载了设置模块。而不是

    env = DJANGO_SETTINGS_MODULE='adtor.settings.production'
    

    我用过

    env = DJANGO_SETTINGS_MODULE=adtor.settings.production
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-01
      • 2017-09-17
      • 1970-01-01
      • 2021-07-14
      • 2017-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多