Django Gunicorn导入错误:没有模块名称wsgi

时间:2017-09-02 17:47:56

标签: python django nginx

我使用Python3.5.2 Django1.9

我使用python -m venv venv/weather_station创建虚拟环境(/ home / user / venv)

这是我在Ubuntu / home / user / myproject中的项目树: (export project=/home/user/myproject

myproject
|
├── gunicorn.conf.py
├── static
│   ├── admin
|
└── weather_station
    ├── chart
    |     ├──views.py
    ├── base
    │   ├── migrations
    │   ├── static
    │   └── templates
    └── weather_station
        ├── __init__.py
        ├── wsgi.py
        ├── urls.py
        ├── settings
            ├── base.py
            ├── production.py

init .py:

import pymysql
pymysql.install_as_MySQLdb()

在wsgi.py中:

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "weather_station.settings.production")

application = get_wsgi_application()

settings.base.py中的部分:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

ROOT_URLCONF = 'weather_station.urls'

WSGI_APPLICATION = 'weather_station.wsgi.application'

STATIC_URL = '/static/'

在settings.production.py的部分内容中:     来自.base import *

DEBUG = False

STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'static')

MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'media')

在gunicorn.conf.py中:     import os

bind = '127.0.0.1:8080'
worders = (os.sysconf('SC_NPROCESSORS_ONLN') * 2) + 1
loglevel = 'error'
command = '/home/user/venv/weather_station/bin/gunicorn'
pythonpath = '/home/user/myproject/weather_station'

在/etc/nginx/sites-available/weather.conf中:

upstream weather_station {
server 127.0.0.1:8080;

}

server {
    listen 80 default_server;
    listen 443 default ssl;
    server_name http://my_ip;
    client_max_body_size 10M;
    keepalive_timeout    15;

    location /static/ {
        alias           /$project/static/;
    }
    location /media/ {
        alias           /$project/media/;
    }

    location / {
        proxy_redirect      off;
        proxy_set_header    Host                    $host;
        proxy_set_header    X-Real-IP               $remote_addr;
        proxy_set_header    X-Forwarded-For         $proxy_add_x_forwarded_for;
        proxy_set_header    X-Forwarded-Protocol    $scheme;
        proxy_pass          http://myproject;

当我运行gunicorn -c gunicorn.conf.py weather_station.wsgi

显示ImportError: No module named 'weather_station.wsgi'

有没有人知道原因?

修改

我也运行gunicorn --pythonpath /home/user/myproject/weather_station -c gunicorn.conf.py weather_station.wsgi

错误与上述相同

但是,我在myproject下运行gunicorn -c gunicorn.conf.py weather_station.weather_station.wsgi

成功找到weather_station.wsgi。

但新错误为ImportError: No module named 'weather_station.settings'

详细日志

File "/home/user/myproject/weather_station/weather_station/wsgi.py", line 17, in <module>
    application = get_wsgi_application()
  File "/home/user/.local/lib/python3.5/site-packages/django/core/wsgi.py", line 13, in get_wsgi_application
    django.setup()
  File "/home/user/.local/lib/python3.5/site-packages/django/__init__.py", line 17, in setup
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
  File "/home/user/.local/lib/python3.5/site-packages/django/conf/__init__.py", line 55, in __getattr__
    self._setup(name)
  File "/home/user/.local/lib/python3.5/site-packages/django/conf/__init__.py", line 43, in _setup
    self._wrapped = Settings(settings_module)
  File "/home/user/.local/lib/python3.5/site-packages/django/conf/__init__.py", line 99, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
ImportError: No module named 'weather_station.settings'

1 个答案:

答案 0 :(得分:1)

下面应该适合你

gunicorn --pythonpath /home/user/myproject/weather_station -c gunicorn.conf.py weather_station.wsgi

问题似乎是在导入命令行weather_station.wsgi时没有应用pythonpath。所以你需要在配置之前设置它