[Fixed]-Django, pyenv, uwsgi – ModuleNotFoundError: No module named 'django'

33👍

The problem was that the system-wide python version linked to uwsgi needs to be the same as the one of the virtualenv, which, I think, is a very stupid thing.

EDIT April 2021: I now recommend using gunicorn, which does not have this problem

3👍

In my case it was using the system wide uwsgi, I’m working using a virtualenv so if I execute

$ which uwsgi

I got /usr/local/python3.6/bin/uwsgi

As Valentin Iovene suggests you need to use the uwsgi from your virtual environment

My directories structure is something like this:

~/Env
--/app
--/bin
----/....
----/uwsgi <-- This should be the good one
----/...
--/include
--/lib

(The app directory is where my django app resides)

In my case uwsgi file hasn’t execution permissions so I only executed:

$ chmod +x ~/Env/bin/uwsgi

Finally under my app directory I executed the uwsgi command as follows:

../bin/uwsgi --http :8000 --module app.wsgi

Now I can see my app working now 🙂

I’m following this guide: https://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html

Next steps are configuring nginx and https

I know it’s a late response but hope this helps and shared what worked for me.

👤AlexWS

2👍

I also found some possible pitfalls I want to share:

  • check, if virtualenv (which is the same as venv, pyhome and home) is set to that directory, that contains the bin, include, lib, … directories
  • check, if the user (uid) can read the files in your project and the libs in virtual environment (this ends in a ModuleNotFoundError instead of a permission error)
  • use need-app to exit on failures (this helps for debugging and should be default imho)
  • use strict to avoid typos in config (this should also be default…)
  • if your test.py runs, try to import modules of your project and your virtual environment, to test if that works. It also helps to add a

    import sys
    print(sys.path)
    

You can also copy the printed sys.path, open a python shell and set sys.path to the same value and try to import the desired wsgi module.

1👍

look at this at gsd.ini:

virtualenv = /home/toogy/.pyenv/versions/%n

have you install django under this virtualenv?

1👍

When I execute:

uwsgi --socket client_book.sock --module myproject.wsgi --chmod-socket=666 

in the directory ~/, and I would get the following error:

ModuleNotFoundError: No module named 'myproject.wsgi'

The solution was to run the command one layer deeper in the directory ~/myproject

This way, uwsgi was able to find myproject.wsgi.

This was what fixed it for me.

Leave a comment