Jonathan Cheng
Jonathan Cheng

Reputation: 479

Django Gunicorn Import Error: no module name wsgi

I use Python3.5.2 Django1.9

I use python -m venv venv/weather_station to create virtual evnironment (/home/user/venv)

This is my project tree in 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

In the init.py:

import pymysql
pymysql.install_as_MySQLdb()

In the 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()

The partial in the 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/'

In the partial of settings.production.py: from .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')

In the 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'

And in the /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;

When I run gunicorn -c gunicorn.conf.py weather_station.wsgi

It showed ImportError: No module named 'weather_station.wsgi'

Does any know the reason ?

EDIT

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

The error is same as the above

However,while I run gunicorn -c gunicorn.conf.py weather_station.weather_station.wsgi under myproject

it successfully weather_station.wsgi.

But the new error is ImportError: No module named 'weather_station.settings'

detailed log

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'

Upvotes: 1

Views: 1589

Answers (1)

Tarun Lalwani
Tarun Lalwani

Reputation: 146630

Below should work for you

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

The problem seems that pythonpath doesn't get applied while importing that command line weather_station.wsgi. So you need to set it before the config also

Upvotes: 2

Related Questions