[Fixed]-Django + virtualenv + gunicorn – No module named django.core.wsgi?

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)

πŸ‘€Dhia

11πŸ‘

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

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

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
πŸ‘€Diablo

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

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

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

πŸ‘€Zach Bellay

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

πŸ‘€Alin

Leave a comment