ImportError: No module named django.core.wsgi Apac

2019-01-08 15:30发布

I am trying to publish my site on an Amazon's EC2 Instance, and I keep getting a 500 error. I really dunno why.

//Log Files

    [Sun Feb 17 23:12:48.066802 2013] mod_wsgi (pid=2102): Target WSGI script '/srv/www/app/poka/apache/wsgi.py' cannot be loaded as Python module.
    [Sun Feb 17 23:12:48.066840 2013] mod_wsgi (pid=2102): Exception occurred processing WSGI script '/srv/www/app/poka/apache/wsgi.py'.
    [Sun Feb 17 23:12:48.066864 2013] Traceback (most recent call last):
    [Sun Feb 17 23:12:48.066889 2013] File "/srv/www/mysite/poka/apache/wsgi.py", line 26, in <module>
    [Sun Feb 17 23:12:48.066920 2013] from django.core.wsgi import get_wsgi_application
    [Sun Feb 17 23:12:48.066945 2013] ImportError: No module named django.core.wsgi

//Apache Config Files

    WSGIScriptAlias / /srv/www/app/mysite/apache/wsgi.py

    WSGIDaemonProcess mysite python-path=/srv/www/app/mysite:/home/ec2-user/.virtualenvs/mysite-main/lib/python2.7/site-packages
    WSGIProcessGroup mysite

    <Directory /srv/www/app/mysite/apache/>
    <Files wsgi.py>
    Order deny,allow
    Allow from all
    </Files>
    </Directory>

    <Directory /srv/www/app/mysite/apache/>
    Order deny,allow
    Allow from all
    </Directory>

    <Directory /home/ec2-user/app/mysite/static>
    Order deny,allow
    Allow from all
    </Directory>

    <Directory /home/ec2-user/app/mysite/media>
    Order deny,allow
    Allow from all
    </Directory>

//wsgi.py

    import os
    import sys
    import site

    site.addsitedir('/home/ec2-user/.virtualenvs/mysite-main/lib/python2.7/site-packages')
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")

    from django.core.wsgi import get_wsgi_application
    application = get_wsgi_application()

    path = '/srv/www/app/mysite'

    if path not in sys.path:
        sys.path.append(path)

8条回答
Ridiculous、
2楼-- · 2019-01-08 15:50

For me, this indicated Django wasn't installed on the sever. Fixed via

pip install Django
查看更多
ら.Afraid
3楼-- · 2019-01-08 15:51

I know that this is an old thread but I've just bumped into the same issue and I don't think that this is caused by a missing package. As the Django core distribution contains the correct wsgi handler already.

The problem here is that when wsgi.py is executed it's missing the packages of the site-packages from your virtualenv. (If you have activated your virtualenv, and done pip install django then everything is fine. You have the necessary django packages).

As far as I'm concerned, I fixed the issue modifying the sys.path in my Path/to/Project/Project/wsgi.py file.

You have to append your project dir and your virtualenv site-packages to the sys.path List. Here is my wsgi.py file contained in my project (Talking about the wsgi.py created with django-admin.py start-project)... that I had to modify in order to make it work with Apache

# =====================
# wsgi.py file begin 

import os, sys
# add the hellodjango project path into the sys.path
sys.path.append('<PATH_TO_MY_DJANGO_PROJECT>/hellodjango')

# add the virtualenv site-packages path to the sys.path
sys.path.append('<PATH_TO_VIRTUALENV>/Lib/site-packages')

# poiting to the project settings
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hellodjango.settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

# wsgi.py file end
# ===================

Make sure:

  1. you added mod_wsgi to the Apache modules dir mod_wsgi must be compiled for the OS, Apache and Python version you have

  2. added the load module command into your httpd.conf to load mod_wsgi module LoadModule wsgi_module modules/mod_wsgi.so

  3. configured Django specifics in your httpd.conf or any conf you include in your httpd.conf

Based on the documentation How to use Django with Apache and mod_wsgi

WSGIScriptAlias / <PATH_TO_PROJECT>/hellodjango/hellodjango/wsgi.py
WSGIPythonPath <PATH_TO_PROJECT>:<PATH_TO_VIRTUALENV>/Lib/site-packages

<Directory <PATH_TO_PROJECT>/hellodjango/hellodjango> 
  <Files wsgi.py>
    Order deny,allow
    Require all granted
  </Files>
</Directory>

Hope this helps. It worked for me.

查看更多
唯我独甜
4楼-- · 2019-01-08 15:59

Make sure that you are using the uwsgi module installed inside the virtualenv, for exemple:

/home/ubuntu/venv/bin/uwsgi --ht :8000 --module mysite.wsgi
查看更多
萌系小妹纸
5楼-- · 2019-01-08 16:04

Add this to the Apache configuration file:

WSGIPythonHome /home/ec2-user/.virtualenvs/mysite-main
查看更多
爷的心禁止访问
6楼-- · 2019-01-08 16:11

I had a similar error just now. It turns out that our Django code was developed on python 3.5, but for some reasons the people who deployed our server setup virtualEnv with python 2.7. We redeployed with python 3.5 and everything worked for us

Below was the error message I received:

$ python serviceStartup.py 
Traceback (most recent call last):
  File "serviceStartup.py", line 10, in <module>
    from django.core.wsgi import get_wsgi_application
ImportError: No module named django.core.wsgi

Hope this will help with anyone seeing a similar error message!

查看更多
姐就是有狂的资本
7楼-- · 2019-01-08 16:13

At first glance, I am sorry for my English. I also faced this issue, and I have solved it by changing 'wsgi.py' file to:

import os
import django
from django.core.handlers.wsgi import WSGIHandler


os.environ.setdefault("DJANGO_SETTINGS_MODULE", "eisentask.settings.production")
django.setup(set_prefix=False)

application = WSGIHandler()
查看更多
登录 后发表回答