【问题标题】:Unable to load a Python package via a file无法通过文件加载 Python 包
【发布时间】:2015-08-20 18:25:35
【问题描述】:

我正在尝试加载具有以下内容的文件:

import os, sys
sys.path.append('/opt/graphite/webapp')
os.environ['DJANGO_SETTINGS_MODULE'] = 'graphite.settings'

import django.core.handlers.wsgi

application = django.core.handlers.wsgi.WSGIHandler()

我不断收到此错误:

Target WSGI script '/opt/graphite/conf/graphite.wsgi' cannot be loaded as Python module.

当我在命令行上调出 Python 并尝试按如下方式加载此模块时,我没有收到任何错误:

import django.core.handlers.wsgi

我检查了 django 及其所有子目录的权限。有什么想法吗?

这些是apache中的错误:

Thu Aug 20 14:37:23 2015] [info] [client 26.16.7.183] mod_wsgi (pid=1812, process='graphite', application=''): Loading WSGI script '/opt/graphite/conf/graphite.wsgi'.
[Thu Aug 20 14:37:23 2015] [error] [client 26.16.7.183] mod_wsgi (pid=1812): Target WSGI script '/opt/graphite/conf/graphite.wsgi' cannot be loaded as Python module.
[Thu Aug 20 14:37:23 2015] [error] [client 26.16.7.183] mod_wsgi (pid=1812): Exception occurred processing WSGI script '/opt/graphite/conf/graphite.wsgi'.
[Thu Aug 20 14:37:23 2015] [error] [client 26.16.7.183] Traceback (most recent call last):
[Thu Aug 20 14:37:23 2015] [error] [client 26.16.7.183]   File "/opt/graphite/conf/graphite.wsgi", line 5, in <module>
[Thu Aug 20 14:37:23 2015] [error] [client 26.16.7.183]     import django.core.handlers.wsgi
[Thu Aug 20 14:37:23 2015] [error] [client 26.16.7.183] ImportError: No module named django.core.handlers.wsgi
[Thu Aug 20 14:37:23 2015] [debug] mod_headers.c(743): headers: ap_headers_output_filter()

这是我的 apache 石墨配置文件:

=================

Listen 8090
LoadModule wsgi_module /usr/lib64/apache2/mod_wsgi.so
WSGISocketPrefix /etc/apache2/wsgi
<Directory /opt/graphite/webapp>
        Options All
        AllowOverride All
        Order deny,allow
        Allow from all
</Directory>
DocumentRoot "/opt/graphite/webapp"
#
<VirtualHost *:8090>
        ServerName 192.168.101.2
        Header set Access-Control-Allow-Origin "*"
        DocumentRoot "/opt/graphite/webapp"
        WSGIDaemonProcess graphite processes=20 threads=20 display-name='%{GROUP}' inactivity-timeout=120
        WSGIProcessGroup graphite
#
        WSGIApplicationGroup %{GLOBAL}
        WSGIImportScript /opt/graphite/conf/graphite.wsgi process-group=graphite application-group=%{GLOBAL}
        WSGIScriptAlias / /opt/graphite/conf/graphite.wsgi
        Alias /static/ /opt/graphite/webapp/content/
        <Location "/content/">
                 SetHandler None
        </Location>
#
        Alias /media/ "/usr/local/lib64/python2.6/site-packages/django/contrib/admin/media/"
#
        <Location "/media/">
                 SetHandler None
        </Location>
        <Directory /opt/graphite/conf/>
           Allow from all
        </Directory>
#
LogLevel debug
ErrorLog /var/log/apache2/graphite_error
</VirtualHost>

它一直给我关于这个文件的错误:

/opt/graphite/conf/graphite.wsgi

[Fri Aug 21 13:04:52 2015] [info] [client 29.0.213.18] mod_wsgi (pid=11626, process='graphite', application=''): Loading WSGI script '/opt/graphite/conf/graphite.wsgi'.
[Fri Aug 21 13:04:52 2015] [error] [client 29.0.213.18] mod_wsgi (pid=11626): Target WSGI script '/opt/graphite/conf/graphite.wsgi' cannot be loaded as Python module.
[Fri Aug 21 13:04:52 2015] [error] [client 29.0.213.18] mod_wsgi (pid=11626): Exception occurred processing WSGI script '/opt/graphite/conf/graphite.wsgi'.

当我在 shell 上并发出问题时:

python /opt/graphite/conf/graphite.wsgi

我没有错误。不知道这里有什么问题?

【问题讨论】:

    标签: python graphite


    【解决方案1】:

    您的问题很可能是因为 mod_wsgi 被编译并绑定到与您打算使用不同的 Python 版本/安装。因此,它不会查看您安装所有软件包的位置。

    您想使用哪个 Python 版本以及您要获得什么:

    import sys
    print(sys.version_info)
    print(sys.prefix)
    

    从解释器运行时。

    然后算出正在使用的 Python mod_wsgi 的版本:


    更新 1

    要从命令行 Python 检查 django 模块通常来自哪里,以便您可以查看您是否使用与 mod_wsgi 编译和使用相同的 Python 安装,您可以在解释器中执行以下操作:

    >>> import django
    >>> print django.__file__
    /some/path/lib/python2.7/site-packages/django/__init__.pyc
    

    模块应具有__file__ 属性,但静态链接到 Python 二进制文件的内置模块除外。

    所以django 模块必须有一个。

    【讨论】:

    • python 2.6.9只有一个版本
    • print (sys.prefix) 说 /usr 并且 print(sys.path) 显示了我在 /usr/local/lib64/python2.6/site-packages 和 /usr/lib64/python2 下的包。 6/site-packages/是这样吗?
    • 从你的命令行 Python,如果你导入 django 模块并打印出 django.__file__ 那个目录是什么,它是在 mod_wsgi 下运行的代码的 sys.path 中的父目录。
    • print django._file_ Traceback(最近一次调用最后):文件“”,第 1 行,在 中 AttributeError: 'module' object has no attribute 'file我>'
    • 仍然是接缝错误,我按照文档编译了 mod_wsgi。仍然是“ImportError: No module named django.core.handlers.wsgi”错误
    【解决方案2】:

    您可能正在通过网络服务器或其他方式加载/opt/graphite/conf/graphite.wsgi。你能告诉我们当你遇到这个错误时你做了什么吗?

    您是否检查过/opt/graphite/conf/graphite.wsgi 是否可执行?

    chmod +x /opt/graphite/conf/graphite.wsgi

    import django.core.handlers.wsgi 的导入工作正常。但这不是错误所在。实际的错误是,无论您使用什么都无法将文件 /opt/graphite/conf/graphite.wsgi 作为 python 模块而不是任何 django 模块加载。

    【讨论】:

    • -rwxr-xr-x 1 root root 204 Aug 20 13:48 /opt/graphite/conf/graphite.wsgi
    • 是的,我正在尝试通过 http 加载此文件。我将错误放在帖子中。
    • 好的,现在我知道你为什么要测试 django 的导入了…… :) 你是在使用 virtualenv 还是安装了 django gobally?如果使用 virtualenv,您必须在 apache 配置中将 WSGIPythonHome 设置为正确的 virtualenv。
    • 任何想法如何解决这个问题?
    • 抱歉,点击返回太快 :) 见上面的评论
    【解决方案3】:

    作为 Python 新手,但最近与 Netbeans 进行了斗争,试图找出 java 模块加载错误的原因,我了解到我的 Ubuntu 14.04 机器上存在多个 java 版本。例如, ~$ whereis python -- 告诉我多个位置 ~$ which python -- 给了我 /usr/bin/python

    那么,您的 IDE 可以使用不同的 python 吗?在终端中找到路径但在 IDE 中没有找到的路径?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-03
      • 2018-10-23
      • 1970-01-01
      • 1970-01-01
      • 2020-01-10
      • 1970-01-01
      • 2021-11-06
      • 2020-08-27
      相关资源
      最近更新 更多