121👍
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
6👍
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.
- [Django]-Problems extend change_form.html in django admin
- [Django]-Chaining multiple filter() in Django, is this a bug?
- [Django]-How to obtain and/or save the queryset criteria to the DB?
3👍
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
- [Django]-Charts in django Web Applications
- [Django]-How to repeat a "block" in a django template
- [Django]-How can I get the full/absolute URL (with domain) in Django?
1👍
If you are using supervisor
then you have to set environment
in supervisor config file as bellow.
environment=HOME="/home/to/your/project/root"
- [Django]-Setting Django up to use MySQL
- [Django]-Using Django time/date widgets in custom form
- [Django]-What is the most efficient way to store a list in the Django models?
0👍
I faced a similar problem. The gunicorn was being run by a globally installed package, not the one that was installed in the virtual environment. This answer helped me to figure it out.
- [Django]-SocketException: OS Error: Connection refused, errno = 111 in flutter using django backend
- [Django]-How to stop autopep8 not installed messages in Code
- [Django]-Cancel an already executing task with Celery?
0👍
I was facing similar issue. I recreated the virtual environment and install gunicorn using pip3 (not using apt) and it worked fine
- [Django]-How to allow only one radio button to be checked?
- [Django]-Django form: what is the best way to modify posted data before validating?
- [Django]-How to set up a PostgreSQL database in Django