[英]ImportError: No module named django.core.wsgi (ubuntu)
I am following this guide to configure apache for my django web application: https://docs.djangoproject.com/en/1.9/howto/deployment/wsgi/modwsgi/#basic-configuration我正在按照本指南为我的 django Web 应用程序配置 apache: https : //docs.djangoproject.com/en/1.9/howto/deployment/wsgi/modwsgi/#basic-configuration
I have installed Django , mod_wsgi-express (the new way) and mod_wsgi for apache on an ubuntu 15.10 server using:我已经使用以下方法在 ubuntu 15.10 服务器上为 apache安装了Django 、 mod_wsgi-express (新方法)和mod_wsgi :
pip install Django
pip install mod_wsgi
sudo aptitude install libapache2-mod-wsgi
Next I have added the below to: /etc/apache2/apache2.conf接下来我将以下内容添加到: /etc/apache2/apache2.conf
WSGIScriptAlias / /home/user/mysite/mysite/wsgi.py
WSGIPythonPath /home/user/mysite:/home/user/.local/lib/python2.7/site-packages
<Directory /home/user/mysite/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
But when I start apache I get the below error:但是当我启动 apache 时,出现以下错误:
[Sat Mar 26 12:10:06.876025 2016] [wsgi:error] [pid 10622:tid 139806697674496] [client 217.40.250.17:52124] from django.core.wsgi import get_wsgi_application
[Sat Mar 26 12:10:06.876057 2016] [wsgi:error] [pid 10622:tid 139806697674496] [client 217.40.250.17:52124] ImportError: No module named django.core.wsgi
But that module indeed exists in the path I have supplied in WSGIPythonPath above:但是该模块确实存在于我在上面WSGIPythonPath 中提供的路径中:
~/.local/lib/python2.7/site-packages/django/core $ cat wsgi.py
import django
from django.core.handlers.wsgi import WSGIHandler
And:并且:
~/.local/lib/python2.7/site-packages $ ll
total 24
drwx------ 6 user user 4096 Mar 24 22:25 ./
drwx------ 3 user user 4096 Mar 20 20:56 ../
drwxrwxr-x 17 user user 4096 Mar 24 22:25 django/
drwxrwxr-x 2 user user 4096 Mar 24 22:25 Django-1.9.4.dist-info/
drwxrwxr-x 5 user user 4096 Mar 20 20:59 mod_wsgi/
drwxrwxr-x 2 user user 4096 Mar 20 20:59 mod_wsgi-4.4.22.egg-info/
I also tried to validate the version of python the mod_wsgi module was build against:我还尝试验证 mod_wsgi 模块所针对的 python 版本:
/usr/lib/apache2/modules $ ldd mod_wsgi.so
linux-vdso.so.1 => (0x00007ffe50f31000)
libpython2.7.so.1.0 => /usr/lib/x86_64-linux-gnu/libpython2.7.so.1.0 (0x00007f376fbc2000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f376f9a4000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f376f5d9000)
libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007f376f3bf000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f376f1bb000)
libutil.so.1 => /lib/x86_64-linux-gnu/libutil.so.1 (0x00007f376efb7000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f376ecaf000)
/lib64/ld-linux-x86-64.so.2 (0x00005609838b3000)
So it appears it was build against Python 2.7 which is the same version Django is being installed under.因此,它似乎是针对 Python 2.7 构建的,而 Python 2.7 与安装 Django 的版本相同。
So why does apache fail to load my application/django?那么为什么 apache 无法加载我的应用程序/django?
Based on: http://www.webforefront.com/django/setupapachewebserverwsgi.html基于: http : //www.webforefront.com/django/setupapachewebserverwsgi.html
the www-data user (user running apache folder) must have read access to the folder containing the django installation. www-data用户(运行 apache 文件夹的用户)必须对包含 django 安装的文件夹具有读取权限。 I just did a test where I copied the django installation and my web application to: /var/www/test and changed the permission for that folder:
我刚刚做了一个测试,我将 django 安装和我的 web 应用程序复制到: /var/www/test并更改了该文件夹的权限:
sudo chgrp -R www-data /var/www/test
sudo chmod -R g+rwx /var/www/test
and the application now loads.应用程序现在加载。
I got this error just now when I was trying to set up my Django, Nginx and WSGI according to this tutorial: https://uwsgi.readthedocs.io/en/latest/tutorials/Django_and_nginx.html我刚刚根据本教程尝试设置 Django、Nginx 和 WSGI 时遇到此错误: https : //uwsgi.readthedocs.io/en/latest/tutorials/Django_and_nginx.html
When I typed in the command: uwsgi --http :8000 --module mysite.wsgi
当我输入命令时:
uwsgi --http :8000 --module mysite.wsgi
I get the error:我收到错误:
from django.core.wsgi import get_wsgi_application ImportError: No module named django.core.wsgi
from django.core.wsgi import get_wsgi_application ImportError: No module named django.core.wsgi
It made no sense to me because when I entered the python shell in my virtualenv and imported the module, it worked perfect.这对我来说毫无意义,因为当我在 virtualenv 中进入 python shell 并导入模块时,它运行得很好。
However, I found this tutorial to be useful in getting rid of the error: https://gist.github.com/evildmp/3094281但是,我发现本教程对于消除错误很有用: https : //gist.github.com/evildmp/3094281
uwsgi --http :8000 --module project.wsgi --virtualenv /path/to/virtualenv
Typing in the path to your virtualenv causes WSGI to find the module it was missing.输入 virtualenv 的路径会导致 WSGI 找到它丢失的模块。
You can find the path to the virtualenv by typing in $ECHO virtualenv
in your command prompt / terminal.您可以通过在命令提示符/终端中输入
$ECHO virtualenv
来找到 virtualenv 的路径。
I had the same issue.我有同样的问题。
Reason being that I had installed gunicorn via apt, which was then running in the system-wide Python 2 installation, instead of my venv.原因是我通过 apt 安装了 gunicorn,然后它在系统范围的 Python 2 安装中运行,而不是我的 venv。
Fixed it by uninstalling the system gunicorn, and then activating my venv, and installing gunicorn to the venv using pip install gunicorn.通过卸载系统 gunicorn,然后激活我的 venv,并使用 pip install gunicorn 将 gunicorn 安装到 venv 来修复它。 Then it worked straight away.
然后它立即起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.