16

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
Dhia
  • 10,119
  • 11
  • 58
  • 69
Run
  • 54,938
  • 169
  • 450
  • 748

7 Answers7

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.

Mesut Tasci
  • 2,970
  • 1
  • 30
  • 36
  • 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. – Carcigenicate Jun 17 '22 at 21:09
  • Gunicorn for python3 is available as deb. Try `apt install gunicorn3` – John Mathew Feb 17 '23 at 11:08
9

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.

Abed
  • 131
  • 1
  • 3
  • I ran into this issue and this answer fixed it for me! Thanks! – Jasper Aug 01 '22 at 11:44
  • 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. – mikey555 Aug 16 '23 at 21:05
5

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
clemens
  • 16,716
  • 11
  • 50
  • 65
Diablo
  • 426
  • 6
  • 10
2

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)

Dhia
  • 10,119
  • 11
  • 58
  • 69
  • 1
    Did you activate your virtualenv? because it looks like it does not find the django modules. – Dhia May 18 '17 at 22:19
  • but it is strange that it works on my production server. not on my local server – Run May 18 '17 at 22:23
  • 2
    check here it may be a similar problem http://stackoverflow.com/a/14196195/5658350 – Dhia May 18 '17 at 22:24
  • got it running now after rebooting my local machine. not sure it is django or python issue. they are just annoying! – Run May 19 '17 at 05:53
  • 1
    Explicitly specifying "application" is unnecessary. See http://docs.gunicorn.org/en/stable/run.html#django. That makes this answer basically a non-answer. – Bobort Dec 23 '18 at 07:49
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.

leggewie
  • 242
  • 2
  • 9
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
HoangYell
  • 4,100
  • 37
  • 31
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

Dharman
  • 30,962
  • 25
  • 85
  • 135
Zach Bellay
  • 152
  • 2
  • 7