2017-09-02 163 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'遇到了新的錯誤。 –

相關問題