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

9

19

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
Rafaello answered 18/5, 2017 at 22:5 Comment(4)
What's the output of which gunicorn, which pip, and pip freeze...there might be different Pythons or environments that the commands are hittingFleisig
@NickT please see my edit above.Rafaello
can you share the full path of your wsgi File!?Virginium
@DhiaTN it's /var/www/html/django-project/helloapp/helloapp/wsgi.py.Rafaello
V
3

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)

Virginium answered 18/5, 2017 at 22:10 Comment(5)
Did you activate your virtualenv? because it looks like it does not find the django modules.Virginium
but it is strange that it works on my production server. not on my local serverRafaello
check here it may be a similar problem https://mcmap.net/q/324711/-importerror-no-module-named-django-core-wsgi-for-uwsgiVirginium
got it running now after rebooting my local machine. not sure it is django or python issue. they are just annoying!Rafaello
Explicitly specifying "application" is unnecessary. See docs.gunicorn.org/en/stable/run.html#django. That makes this answer basically a non-answer.Steen
G
12

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.

Gupta answered 24/1, 2021 at 8:59 Comment(2)
I ran into this issue and this answer fixed it for me! Thanks!Interest
Thank you. In my case I was able to override the global gunicorn by installing gunicorn in my venv with pip3 install gunicorn. Then I ran which gunicorn and verified that gunicorn was pointing to my venv's copy.Weighting
C
9

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.

Civism answered 5/3, 2019 at 17:21 Comment(2)
Confusingly, this was necessary for me, even though which gunicorn pointed to the venv version. Just having the global installation messed things up, even though it shouldn't have been used in the first place.Breckenridge
Gunicorn for python3 is available as deb. Try apt install gunicorn3Zakarias
E
6

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
Ectoenzyme answered 19/1, 2018 at 6:0 Comment(0)
V
3

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)

Virginium answered 18/5, 2017 at 22:10 Comment(5)
Did you activate your virtualenv? because it looks like it does not find the django modules.Virginium
but it is strange that it works on my production server. not on my local serverRafaello
check here it may be a similar problem https://mcmap.net/q/324711/-importerror-no-module-named-django-core-wsgi-for-uwsgiVirginium
got it running now after rebooting my local machine. not sure it is django or python issue. they are just annoying!Rafaello
Explicitly specifying "application" is unnecessary. See docs.gunicorn.org/en/stable/run.html#django. That makes this answer basically a non-answer.Steen
E
1

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.

Expedite answered 17/4, 2020 at 7:30 Comment(0)
O
0

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
Orthopterous answered 27/3, 2019 at 15:53 Comment(0)
U
0

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

Ursine answered 4/11, 2021 at 0:49 Comment(0)
Q
0

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

Quickel answered 14/10, 2023 at 5:32 Comment(0)
M
0

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.

Masterpiece answered 5/5 at 14:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.