【问题标题】:Django - ImportError : no module named django.core.wsgiDjango - ImportError:没有名为 django.core.wsgi 的模块
【发布时间】:2018-07-28 05:12:07
【问题描述】:

我正在尝试使用 Apache 和 mod_wsgi 部署我的 Django 应用程序。

我根据以下参考安装和配置它们:- https://modwsgi.readthedocs.io/en/develop/user-guides/quick-installation-guide.html

https://docs.djangoproject.com/en/2.0/howto/deployment/wsgi/modwsgi/

我的 django 项目树 - /home/zahlen

 mysite
├── db.sqlite3
├── log
│   ├── access.log
│   └── error.log
├── manage.py
├── mysite
│   ├── __init__.py
│   ├── settings.py
│   ├── static
│   ├── templates
│   ├── urls.py
│   ├── views.py
│   ├── wsgi.py
├── places
│   ├── admin.py
│   ├── apps.py
│   ├── __init__.py
│   ├── migrations
│   ├── models.py
│   ├── static
│   ├── templates
│   ├── tests.py
│   ├── urls.py
│   ├── views.py
└── static

django 存在于/home/zahlen/.local/lib/site-packages

//apache2/apache2.conf

....
....
LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so
WSGIPythonPath /home/zahlen/mysite:/home/zahlen/.local/lib/python2.7/site-packages
<Directory /home/zahlen/mysite/mysite>
    <Files wsgi.py>
        Require all granted
    </Files>
</Directory>

//apache2/sites-available/000-default.conf 虚拟主机配置

<VirtualHost *:80>
    ServerName www.mysite.com
    ServerAlias mysite.com
    ServerAdmin webmaster@mysite.com
    Alias /static /home/zahlen/mysite/mysite
    <Directory /home/zahlen/mysite/static>
        Require all granted
    </Directory>
    ErrorLog /home/zahlen/mysite/log/error.log
    CustomLog /home/zahlen/mysite/log/access.log combined

    <Directory /home/zahlen/mysite/mysite/>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>

    WSGIDaemonProcess mysite.com python-path=/home/zahlen/mysite
    WSGIProcessGroup mysite.com
    WSGIScriptAlias / /home/zahlen/mysite/mysite/wsgi.py
</VirtualHost>

Django 和 mod_wsgi 配置为使用 python2.7 . Vitualenv 被使用并且 Apache 使用 Event MPM

所有这些都在 Ubuntu 服务器虚拟机上。

//log/apache2/error.log 给出以下行来验证 python2.7 的使用

....
[Sat Feb 17 02:16:45.475182 2018] [mpm_event:notice] [pid 7282:tid 140680615626624] AH00489: Apache/2.4.18 (Ubuntu) OpenSSL/1.0.2g mod_wsgi/4.3.0 Python/2.7.12 configured -- resuming normal operations
....

/home/zahlen/mysite/mysite/wsgi.py

import os,sys
sys.path.append('/home/zahlen/mysite')
sys.path.append('/home/zahlen/mysite/mysite')
sys.path.append('/home/zahlen/.local/lib/python2.7/site-packages')
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")

application = get_wsgi_application()

每次我使用虚拟机的 IP 地址打开我的网站时,我的浏览器都会在主机上引发内部服务器错误

//mysite/log/error.log

[Sat Feb 17 02:13:55.862423 2018] [wsgi:error] [pid 7152:tid 139643385603840] [remote 192.168.56.1:46153] mod_wsgi (pid=7152): Target WSGI script '/home/zahlen/mysite/mysite/wsgi.py' cannot be loaded as Python module.
[Sat Feb 17 02:13:55.862536 2018] [wsgi:error] [pid 7152:tid 139643385603840] [remote 192.168.56.1:46153] mod_wsgi (pid=7152): Exception occurred processing WSGI script '/home/zahlen/mysite/mysite/wsgi.py'.
[Sat Feb 17 02:13:55.862579 2018] [wsgi:error] [pid 7152:tid 139643385603840] [remote 192.168.56.1:46153] Traceback (most recent call last):
[Sat Feb 17 02:13:55.862619 2018] [wsgi:error] [pid 7152:tid 139643385603840] [remote 192.168.56.1:46153]   File "/home/zahlen/mysite/mysite/wsgi.py", line 12, in <module>
[Sat Feb 17 02:13:55.862743 2018] [wsgi:error] [pid 7152:tid 139643385603840] [remote 192.168.56.1:46153]     from django.core.wsgi import get_wsgi_application
[Sat Feb 17 02:13:55.862781 2018] [wsgi:error] [pid 7152:tid 139643385603840] [remote 192.168.56.1:46153] ImportError: No module named django.core.wsgi

似乎 wsgi.py 无法导入 django 包,但它存在于 /home/zahlen/.local/lib/site-packages/python2.7 目录中

谁能帮我解决这个问题?

【问题讨论】:

    标签: python django apache mod-wsgi


    【解决方案1】:

    在导入 Django 模块之前,在wsgi.py 中设置sys.path

    【讨论】:

    • 目录/home/zahlen/.local的权限是什么?
    • 仅为 //python2.7 目录设置了正确的权限,而 /home/zahlen/.local 设置为 drwx------ 5 zahlen zahlen。递归地将其更改为drwxrwx--- 5 zahlen www-data 并解决了该问题。谢谢!
    猜你喜欢
    • 2015-01-18
    • 2015-03-19
    • 2016-07-14
    • 1970-01-01
    • 2013-02-02
    • 2017-10-18
    • 1970-01-01
    • 2012-10-21
    相关资源
    最近更新 更多