如何用gunicorn运行这个Django服务器?

时间:2018-02-22 00:35:54

标签: python django gunicorn wsgi

我试图用Gunicorn网络服务器运行我的Django应用程序。但是我很难这样做。

我的目录结构如下所示:

$ pwd
/Users/my_user/my_django_project

$ ls -R
./MySite:
__init__.py         migration_settings.py   urls.py
local_settings.py   settings.py             wsgi.py

./app1:
<blah> <blah> <blah>

./app2:
<blah> <blah> <blah>

./app3:
<blah> <blah> <blah>

这就是我运行Django测试服务器的方式,它可以工作:

$ ./manage.py runserver --settings=MySite.local_settings 0.0.0.0:8000

如何使用gunicorn而不是./manage.py运行相同的服务器?我所看到的一切表明我应该做以下事情。但正如你所看到的,它给了我一个错误:

$ pwd
/Users/my_user/my_django_project

$ gunicorn -v
gunicorn (version 19.7.1)

$ DJANGO_SETTINGS_MODULE=MySite.local_settings gunicorn MySite.wsgi:application --log-file=-

[2018-02-21 19:26:01 -0500] [9038] [INFO] Starting gunicorn 19.7.1
[2018-02-21 19:26:01 -0500] [9038] [INFO] Listening at: http://127.0.0.1:8000 (9038)
[2018-02-21 19:26:01 -0500] [9038] [INFO] Using worker: sync
[2018-02-21 19:26:01 -0500] [9041] [INFO] Booting worker with pid: 9041
[2018-02-21 19:26:01 -0500] [9041] [ERROR] Exception in worker process
Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/gunicorn/arbiter.py", line 578, in spawn_worker
    worker.init_process()
  File "/Library/Python/2.7/site-packages/gunicorn/workers/base.py", line 126, in init_process
    self.load_wsgi()
  File "/Library/Python/2.7/site-packages/gunicorn/workers/base.py", line 135, in load_wsgi
    self.wsgi = self.app.wsgi()
  File "/Library/Python/2.7/site-packages/gunicorn/app/base.py", line 67, in wsgi
    self.callable = self.load()
  File "/Library/Python/2.7/site-packages/gunicorn/app/wsgiapp.py", line 65, in load
    return self.load_wsgiapp()
  File "/Library/Python/2.7/site-packages/gunicorn/app/wsgiapp.py", line 52, in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/Library/Python/2.7/site-packages/gunicorn/util.py", line 352, in import_app
    __import__(module)
  File "/Users/my_user/my_django_project/MySite/wsgi.py", line 13, in <module>
    from django.core.wsgi import get_wsgi_application
ImportError: No module named django.core.wsgi
[2018-02-21 19:26:01 -0500] [9041] [INFO] Worker exiting (pid: 9041)
[2018-02-21 19:26:01 -0500] [9038] [INFO] Shutting down: Master
[2018-02-21 19:26:01 -0500] [9038] [INFO] Reason: Worker failed to boot.

为什么抱怨django.core.wsgi没有找到?正如您所看到的,当我以交互方式从shell提示符运行时,我发现了它:

$ pip install django
Requirement already satisfied: django in /Users/my_user/my_virtual_env/lib/python2.7/site-packages


$ python
Python 2.7.10 (default, Feb  7 2017, 00:08:15)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> import django.core
>>> import django.core.wsgi
>>> from django.core.wsgi import get_wsgi_application
>>>

我在这里做错了什么以及如何解决?

0 个答案:

没有答案