Gunicorn, no module named 'myproject
For my side, My project structure is
myproject
├── manage.py
├── myproject
│ ├── wsgi.py
│ ├── ..
Dockerfile
docker-composer.yml
So in docker-composer.yml, when command
gunicorn myproject.wsgi:application --bind 0.0.0.0:8000
i get following error
ModuleNotFoundError: No module named 'myproject.wsgi'
What we have to do is, we must run gunicorn command inside folder, not project root. This is the working code
sh -c "cd ./myproject && gunicorn myproject.wsgi:application --bind 0.0.0.0:8000"
Before gunicorn command, we have to change directory with "cd ./project". Inside the "myproject" directory, gunicorn can recognise our projects clearly.
If you are using supervisor
then you have to set environment
in supervisor config file as bellow.
environment=HOME="/home/to/your/project/root"
Run these command after replacing your python working directory path.
# Go to your current working directory
cd /path/to/folder
# Activate your virtual environment. Ignore if already in activated mode
source /path/to/virtualenv/bin/activate
# Install gunicorn in virtualenv
pip3 install gunicorn
# Run this command. Replace PORT and app name accordingly
gunicorn --bind 0.0.0.0:5000 wsgi:app
Your error message is
ImportError: No module named 'myproject.wsgi'
You ran the app with
gunicorn --bind 0.0.0.0:8000 myproject.wsgi:application
And wsgi.py has the line
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
This is the disconnect. In order to recognize the project as myproject.wsgi
the parent directory would have to be on the python path... running
cd .. && gunicorn --bind 0.0.0.0:8000 myproject.wsgi:application
Would eliminate that error. However, you would then get a different error because the wsgi.py file refers to settings
instead of myproject.settings
. This implies that the app was intended to be run from the root directory instead of one directory up. You can figure this out for sure by looking at the code- if it uses absolute imports, do they usually say from myproject.app import ...
or from app import ...
. If that guess is correct, your correct commmand is
gunicorn --bind 0.0.0.0:8000 wsgi:application
If the app does use myproject
in all of the paths, you'll have to modify your PYTHONPATH to run it properly...
PYTHONPATH=`pwd`/.. gunicorn --bind 0.0.0.0:8000 myproject.wsgi:application