close

[Solved] Gunicorn, no module named ‘myproject

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Gunicorn, no module named ‘myproject in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Gunicorn, no module named ‘myproject Error Occurs?

Today I get the following error Gunicorn, no module named ‘myproject in python.

How To Solve Gunicorn, no module named ‘myproject Error ?

  1. How To Solve Gunicorn, no module named 'myproject Error ?

    To Solve Gunicorn, no module named 'myproject Error What we have to do is, we must run gunicorn command inside folder, not project root. This is the working code

  2. Gunicorn, no module named 'myproject

    To Solve Gunicorn, no module named 'myproject Error What we have to do is, we must run gunicorn command inside folder, not project root. This is the working code

Solution 1


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

Solution 2

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.

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read