Run
Run

Reputation: 57196

django + virtualenv + gunicorn - No module named django.core.wsgi?

I have gunicorn installed inside my virtual env:

$ pip install gunicorn
Collecting gunicorn
  Using cached gunicorn-19.7.1-py2.py3-none-any.whl
Installing collected packages: gunicorn
Successfully installed gunicorn-19.7.1

But when I try run my app with it:

$ gunicorn helloapp.wsgi
[2017-05-18 22:42:36 +0000] [1963] [INFO] Starting gunicorn 19.6.0
[2017-05-18 22:42:36 +0000] [1963] [INFO] Listening at: http://127.0.0.1:8000 (1963)
[2017-05-18 22:42:36 +0000] [1963] [INFO] Using worker: sync
[2017-05-18 22:42:36 +0000] [1967] [INFO] Booting worker with pid: 1967
[2017-05-18 22:42:36 +0000] [1967] [ERROR] Exception in worker process
Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/gunicorn/arbiter.py", line 557, in spawn_worker
    worker.init_process()
  File "/usr/lib/python2.7/dist-packages/gunicorn/workers/base.py", line 126, in init_process
    self.load_wsgi()
  File "/usr/lib/python2.7/dist-packages/gunicorn/workers/base.py", line 136, in load_wsgi
    self.wsgi = self.app.wsgi()
  File "/usr/lib/python2.7/dist-packages/gunicorn/app/base.py", line 67, in wsgi
    self.callable = self.load()
  File "/usr/lib/python2.7/dist-packages/gunicorn/app/wsgiapp.py", line 65, in load
    return self.load_wsgiapp()
  File "/usr/lib/python2.7/dist-packages/gunicorn/app/wsgiapp.py", line 52, in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/usr/lib/python2.7/dist-packages/gunicorn/util.py", line 384, in import_app
    __import__(module)
  File "/var/www/html/django-project/helloapp/helloapp/wsgi.py", line 12, in <module>
    from django.core.wsgi import get_wsgi_application
ImportError: No module named django.core.wsgi
[2017-05-18 22:42:36 +0000] [1967] [INFO] Worker exiting (pid: 1967)
[2017-05-18 22:42:36 +0000] [1963] [INFO] Shutting down: Master
[2017-05-18 22:42:36 +0000] [1963] [INFO] Reason: Worker failed to boot.

What I have done wrong?

This is my app structure:enter image description here

Any ideas?

This is my requirments.txt:

appdirs==1.4.3
Django==1.11.1
gunicorn==19.7.1
packaging==16.8
pyparsing==2.2.0
pytz==2017.2
six==1.10.0

EDIT:

(env) xxx@xxx-desktop:/var/www/html/django-project/helloapp$ which gunicorn
/var/www/html/django-project/helloapp/env/bin/gunicorn

(env) xxx@xxx-desktop:/var/www/html/django-project/helloapp$ which pip
/var/www/html/django-project/helloapp/env/bin/pip

Upvotes: 19

Views: 18844

Answers (9)

Tms91
Tms91

Reputation: 4194

I came across this problem as I was managing a django app developed by third part.

I was trying to start the project "basic-django-ecommerce" by running

PYTHONPATH=`pwd`/ venv/bin/gunicorn basic-django-ecommerce.wsgi:application --bind localhost:8002

in the project folder, being the content of basic-django-ecommerce.wsgi the following

# this file must be given in input to gunicorn, 
# and it is better that remains in root directory of the application.

import os
from django.core.wsgi import get_wsgi_application

# environment settings for Django app
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'basic-django-ecommerce.settings')

# Initialize app Django
application = get_wsgi_application()

In my case the problem was that the project name is "basic-django-ecommerce", but the folder of the main django app created by django-admin startproject basic-django-ecommerce command with the same name, was was manually renamed to "mainapp_ecommerce".

In order to fix it, I have changed the .wsgi file line

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'basic-django-ecommerce.settings')

to

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mainapp_ecommerce.settings')

then renamed the .wsgi file name from basic-django-ecommerce.wsgi to mainapp_ecommerce.wsgi and changed the gunicorn command to

PYTHONPATH=`pwd`/ venv/bin/gunicorn mainapp_ecommerce.wsgi:application --bind localhost:8002

NOTE: I have tryed to change only the line of the .wsgi file, but it was necessary also to change the .wsgi file name to make gunicorn run the app, otherwise the same error is raised yet.

Upvotes: 0

Alin
Alin

Reputation: 313

I used a combination of the answers above and another solution. I deactivated virtual env and Installed Django, then I activated it and ran the command like this: gunicorn --bind 0.0.0.0:8000 myproject.wsgi:application

Upvotes: 0

Zach Bellay
Zach Bellay

Reputation: 362

I am using poetry and pyenv. In my case it turns out the gunicorn instance that I was running was referencing a system level installation, as opposed to the one installed in the virtual environment. I figured this out by running which gunicorn and comparing it to the output of pyenv which gunicorn.

What solved it for me was doing

$(pyenv which gunicorn) <app_name>.wsgi:application --bind 0.0.0.0

Upvotes: 0

Abed
Abed

Reputation: 161

It might not be the case for this particular question However I encountered a similar issue and was led here by google. So I place this answer here in the hope to be useful for others.

The problem for me was that gunicorn was being ran by a globally installed package not the one that was installed in the virtual environment. To make sure that whether this is the case for you or not, just simply run the which gunicorn and check it is coming from your virtualenv bin directory. If it is not coming from your virtual env bin directory follow these steps:

  1. Deactivated the env.
    • deactivate env
  2. Uninstalled the globally installed gunicron
    • pip uninstall gunicorn
  3. Activate the env. [ I use virtualenvwrapper for virtualenv management and I recommend you to do so. ]
    • workon env

Now gunicorn should work as expected.

Upvotes: 12

leggewie
leggewie

Reputation: 272

As pointed out here and elsewhere, the root of the problem as indidcated by the error message is that gunicorn cannot find the django modules. The question then becomes why that is the case and there can be many different reasons. In my case on a Ubuntu bionic host without virtualenv it was due to the fact that I had installed python3-django and gunicorn (the latter being a python2 package).

The solution was to install python3-gunicorn, remove gunicorn and run gunicorn as gunicorn3.

Upvotes: 1

HoangYell
HoangYell

Reputation: 4651

Wrong directory:

check your current directory then cd to the right directory before calling.

Example:

Incorrect:

~/project_name/main$ gunicorn3 --bind 0.0.0.0:8000 main.wsgi:application

Correct:

~/project_name/main$ cd ..
~/project_name$ gunicorn3 --bind 0.0.0.0:8000 main.wsgi:application

Upvotes: 0

Mesut Tasci
Mesut Tasci

Reputation: 3130

I have same issue and I solved it by removing gunicorn which installed with system package manager(apt-get etc).

apt-get installing gunicorn to site-packages of python2 and pip installing Django to site-packages of python3. So Gunicorn and Django not in same site-packages directory. So gunicorn cannot find django. Insalling Gunicorn and Django in same package dir should solve the problem.

Upvotes: 10

Diablo
Diablo

Reputation: 442

In /etc/systemd/system/gunicorn.service, make sure your Working Directory is pointing to your app directory.

[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=sammy
Group=www-data
WorkingDirectory=/home/sammy/myproject
ExecStart=/home/sammy/myproject/myprojectenv/bin/gunicorn --access-logfile - --workers 3 --bind unix:/home/sammy/myproject/myproject.sock myproject.wsgi:application

Upvotes: 6

Dhia
Dhia

Reputation: 10609

you should actually run it as follow:

gunicorn helloapp.wsgi:application 
  • Basic usage of gunicorn:

gunicorn [OPTIONS] APP_MODULE

Where APP_MODULE is of the pattern $(MODULE_NAME):$(VARIABLE_NAME)

Upvotes: 3

Related Questions