【问题标题】:Apache WSGI no module named django whitout virtualenvApache WSGI 没有名为 django whitout virtualenv 的模块
【发布时间】:2016-12-02 16:12:12
【问题描述】:

wsgi.py:

import os,sys

sys.path.append('/var/www/html/yuyyu/yuyyu')

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "yuyyu.settings")

from django.core.wsgi import get_wsgi_application

application = get_wsgi_application()

apache.conf:

...


        WSGIDaemonProcess yuyyu python-path=/var/www/html/yuyyu

        WSGIProcessGroup yuyyu
        WSGIScriptAlias / /var/www/html/yuyyu/yuyyu/wsgi.py \
                 process-group=yuyyu application-group=%{GLOBAL}
 ...

这是我在 error.log 中的错误:

[Fri Dec 02 19:07:55.862266 2016] [:error] [pid 1414] [remote 213.14.244.3:2784] mod_wsgi (pid=1414): Target WSGI script '/var/www/html/yuyyu/yuyyu/wsgi.py' cannot be loaded as Python module.
[Fri Dec 02 19:07:55.862323 2016] [:error] [pid 1414] [remote 213.14.244.3:2784] mod_wsgi (pid=1414): Exception occurred processing WSGI script '/var/www/html/yuyyu/yuyyu/wsgi.py'.
[Fri Dec 02 19:07:55.862353 2016] [:error] [pid 1414] [remote 213.14.244.3:2784] Traceback (most recent call last):
[Fri Dec 02 19:07:55.862393 2016] [:error] [pid 1414] [remote 213.14.244.3:2784]   File "/var/www/html/yuyyu/yuyyu/wsgi.py", line 23, in <module>
[Fri Dec 02 19:07:55.862398 2016] [:error] [pid 1414] [remote 213.14.244.3:2784]     from django.core.wsgi import get_wsgi_application
[Fri Dec 02 19:07:55.862418 2016] [:error] [pid 1414] [remote 213.14.244.3:2784] ImportError: No module named 'django'

我的项目路径是/var/www/html/yuyyu/

我的wsgi文件路径是/var/www/html/yuyyu/yuyyu/

我也得到 500 内部服务器错误和 我没有使用 virtualenv,所以我该怎么做才能解决这个问题?

【问题讨论】:

  • 你用的是什么python版本?您确定为这个精确版本安装了 django 吗? (例如,在为 python2 安装 django 之后,您确定没有尝试使用 python3 运行 django 吗?)您能否为相关的 python 版本添加 pip freeze 输出?您是否还确定如果您为正确的 python 版本安装了django,那么您不是仅为用户安装它,而是为您的整个系统安装它(您没有将--user 标志与pip 一起使用)
  • im using python2 here is pip freeze result: Django==1.9.7 Landscape-Client==14.12 MySQL-python==1.2.3 PAM==0.4.2 Twisted-Core==13.2.0 apt-xapian-index==0.45 argparse==1.2.1 chardet==2.0.1 colorama==0.2.5 configobj==4.7.2 django-secure==1.0.1 django-sslserver==0.19 html5lib==0.999 pyOpenSSL==0.13 pyserial==2.6 python-apt==0.9.3.5ubuntu2 python-debian==0.1.21-nmu2ubuntu2 requests==2.2.1 six==1.5.2 ssh-import-id==3.21 uWSGI==2.0.14 urllib3==1.7.1 wheel==0.24.0 wsgiref==0.1.2
  • 有关使用带有 mod_wsgi 的 Python 虚拟环境的一般信息(推荐),请参阅modwsgi.readthedocs.io/en/develop/user-guides/…
  • 与虚拟环境不同,Python 解释器中的sys.prefix 有什么作用?您有 Django 安装输出吗?然后使用 WSGI hello world 找出 sys.prefix 是什么 mod_wsgi 正在使用。这将确认他们使用的是相同的 Python 安装。

标签: python django apache wsgi


【解决方案1】:
WSGIDaemonProcess yuyyu python-path=/var/www/html/yuyyu

我认为你的问题就在这里,python-path 不应该是你的 django 项目的路径,而是你的 python 库(例如你的 python 安装 django 的站点包目录的路径)。

例如,对我来说,这条路径是/usr/lib/python3.5/site-packages

【讨论】:

  • 我也试试这个WSGIDaemonProcess yuyyu python-path=/var/www/html/yuyyu:python-home=/usr/local/lib/python2.7/site-packages ,但结果是一样的
  • 试试这个 :WSGIDaemonProcess yuyyu python-path=/usr/local/lib/python2.7/site-packages 但错误还是一样
  • WSGIDaemonProcess yuyyu python-path=/var/www/html/yuyyu:python-home=/usr/local/lib/p‌​ython2.7/site-packag‌​es 这个语法不正确。
【解决方案2】:

在 wsgi.py 中尝试加载你有 Django 的路径。在我的配置中,它位于 virtualenv 内部,路径如下所示:

import site
site.addsitedir('~/virtualenvs/myenv/lib/python3.5/site-packages')

【讨论】:

    【解决方案3】:

    这是我的 django 安装到的解决方案

    /usr/lib/python2.7/dist-packages
    

    我像这样更改我的 .conf 文件

    WSGIDaemonProcess yuyyu python-path=/usr/local/lib/python2.7/dist-packages
    WSGIProcessGroup yuyyu
    WSGIScriptAlias / /var/www/html/yuyyu/yuyyu/wsgi.py
    

    【讨论】: