2017-09-02 162 views
0

我用Python3.5.2 Django1.9Django的Gunicorn导入错误:没有模块名称WSGI

我用python -m venv venv/weather_station创建虚拟evnironment(/家/用户/ VENV)

这是在Ubuntu我的项目树/家庭/用户/ 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进口*

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: 进口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'

有没有人知道原因?

EDIT

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

的错误是同上

然而,虽然我下的myproject

它成功地运行weather_station.wsgi gunicorn -c gunicorn.conf.py weather_station.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

我没有看到任何'wsgi.py'或'__init __。py'在你的目录树中。 –

+0

@ Klaus D.I补充说明 –

+0

您是否在运行gunicorn之前激活了环境? –

回答

0

下面应该为你

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

问题的工作似乎PYTHONPATH没有得到应用,同时导入一个命令行weather_station.wsgi。所以你需要在配置之前设置它

+0

我使用上面的命令遇到了同样的错误。但是使用'gunicorn -c gunicorn.conf.py weather_station.weather_station.wsgi'遇到了新的错误。 –

相关问题