简体   繁体   中英

uwsgi + nginx + Django : ImportError: No module named django.core.wsgi

I am trying to deploy a Django app using nginx + uwsgi. I created a virtual environment (virtualenv), and installed both uwsgi and Django inside the virtual env (ielocal to the virtual environment). I have no global Django and uwsgi. When I run the uwsgi --ini project.ini, I am having an 'ImportError: No module named django.core.wsgi' exception:

from django.core.wsgi import get_wsgi_application
ImportError: No module named django.core.wsgi
unable to load app 0 (mountpoint='') (callable not found or import error)
*** no app loaded. going in full dynamic mode ***
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 5987)
spawned uWSGI worker 1 (pid: 5988, cores: 1)
spawned uWSGI worker 2 (pid: 5989, cores: 1)
spawned uWSGI worker 3 (pid: 5990, cores: 1)
spawned uWSGI worker 4 (pid: 5991, cores: 1)

Based on my search, it's recommended to put env and pythonpath variables in the ini if you are using Django1.5 or lower. However, I am using Django 1.7, so I did not place them anymore. Here's my project.ini:

#project.ini file
[uwsgi]

# Django-related settings
# the base directory (full path)
chdir           = /root/virtualenv/project
# Django wsgi file
module          = project.wsgi:application
# the virtualenv (full path)
home            = /root/virtualenv

# process-related settings
# master
master          = true
# maximum number of worker processes
processes       = 4
# the socket (use the full path to be safe
socket          = /root/virtualenv/project/project.sock
# ... with appropriate permissions - may be needed
chmod-socket    = 666
chown-socket    = root:root
# clear environment on exit
vacuum          = true

# other config options
uid = root
gid = root
processes = 4
daemonize = /var/log/uwsgi/project.log
no-site = True

How will i fix this? I'm quite stuck on this for a day already. Any ideas are greatly appreciated. Thanks in advance!

your module is pointed to your project, shouldn't it be pointed to your projects main app that way it can find the wsgi file?

so on my INI file looks like this.

In my particular case I'm using a virtual environment, django 1.7 and uwsgi.

vhost = true
plugins = python
socket = /tmp/noobmusic.sock
master = true
enable-threads = true
processes = 2
wsgi-file = /home/myname/webapps/music/music/music/wsgi.py
virtualenv = /home/myname/webapps/music/musicenv/
chdir = /home/myname/webapps/music/music/

this is the only site I've ever setup uwsgi as I typically use mod-wsgi and unfortunately do not remember all the steps.

I had similar issue. Solved it -after many hours- by making sure that uwsgi is installed using same python version (2 / 3) as the python version of your virtualenv. Otherwise it will not use your virtualenv and thus start throwing 'can not find module xyz' kind of errors. To install uwsgi under python3 you have to use pip3 (which in turn might need to be installed with something like 'apt-get install python-pip3'). When calling uwsgi on cli or via .ini file you need to reference your virtualenv mentioning the full path (which ends one folderlevel above the folder in which the /bin/ is; so /example/myvenv/bin/activate means the full path is /example/myvenv.

I made the uwsgi install global so outside of my virtualenv. I suppose above applies/would work as well when installing uwsgi within the virtualenv, but have not tried (yet).

Keep the system-wide uwsgi the same version as your virtual environment python. In my environment, my virtual environment is python3.7, but the system default python is python3.6. After I uninstall uWSGI , and re-install the system-wide uWSGI with python3.7, the problem has been resolved.

sudo pip uninstall uwsgi
sudo -H python3.7 -m pip install uwsgi

I can't see any problem in your configuration (though I'm not very good at these topics). I can try to advice some steps to localize the problem.

  • Test uwsgi without using virtualenv . Note that the virtual directory is just a directory, so add it to your PYTHONPATH and run uwsgi .

    Before that you can try

    python -c 'import django.core.wsgi'

    If that works, then the problem is in uwsgi virtualenv configuration.

  • Test virtualenv . Run it and check that the module can be imported.

    If that works, then the problem is in uwsgi . Go to the previous case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM