Solving Gunicorn error – no module named module

Gunicorn error – no module named <module> is an error which occurs in python when you run gunicorn command inside the wrong directory.

This post is my attempt to explain to you why this error occurs and how you can solve it, I will also include multiple solutions that could be considered as alternative fixes to the error.

Exploring the Error : Gunicorn error – no module named <module>

This is an error which occurs in python when you run gunicorn command inside the wrong directory.

The error should look like this. Double check in order to avoid mixing between errors.

                                                                       #
Traceback (most recent call last):
    worker.init_process()
    self.load_wsgi()
.....
    __import__(module)
ImportError: No module named 'myproject.wsgi'
[15262] [INFO] Worker exiting (pid: 15262)
[15259] [INFO] Shutting down: Master
[15259] [INFO] Reason: Worker failed to boot.
                                                                       #

Bellow are the solutions which worked for me and will help you to successfully solve your problem.

Solution 1 : change the directory before you run the gunicorn command

If this solution does not fix your problem you can try the solution bellow.

As we said before this problem happens when you run the gunicorn command inside the wrong directory. Usually in the root directory of your project.

To solve the issue, all you have to do is to simply change the directory to the folder in which you have your module then run the gunicorn command.

For example, this is the command causing the error.

                                                                       #
gunicorn --bind 0.0.0.0:8000 myproject.wsgi:application
                                                                       #

The command above should be replaced with the command bellow.

                                                                       #
sh -c "cd ./myproject && gunicorn myproject.wsgi:application --bind 0.0.0.0:8000"
                                                                       #

cd ./ at the beginning is to change the directory.

After trying this solution the error should be gone.

If this solution does not fix your problem you can try the solution bellow.

Solution 2 : run gunicorn inside your activated virtual environment.

The first step is to replace your working python directory path.

Then navigate to the current working directory using the command bellow.

                                                                       #
cd /path/to/folder
                                                                       #

After that you should activate your virtual environment using the command bellow.

                                                                       #
source /path/to/virtualenv/bin/activate
                                                                       #

Now, from the activated virtual environment install gunicorn.

                                                                       #
pip3 install gunicorn
                                                                       #

Run this command.

                                                                       #
gunicorn --bind 0.0.0.0:5000 wsgi:app
                                                                       #

Keep in mind that 5000 is your port name which you can change and add the name of your app at the end.

The solution should be enough, please try the final solution if this fails.

Summing-up

The article is over, I hope I have been able to help you solve this error or at least guide you in the right direction, check out other solutions to different errors you can do that by using the search bar on top of this page.