无法通过文件加载Python包

时间:2015-08-20 18:25:35

标签: python graphite

我正在尝试加载包含以下内容的文件:

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的graphite.conf:

=====

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

我没有错误。不确定这里有什么问题?

3 个答案:

答案 0 :(得分:2)

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

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

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

从解释器运行时。

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

更新1

要检查django模块来自命令行Python的正常位置,以便您可以看到是否使用与mod_wsgi编译和使用相同的Python安装,您将在解释器中执行:< / p>

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

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

所以django模块必须有一个。

答案 1 :(得分:1)

您可能正在通过网络服务器或其他东西加载/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模块。

答案 2 :(得分:0)

刚接触Python,但最近与Netbeans争吵试图弄清楚为什么java模块加载错误,我了解到我的Ubuntu 14.04盒子上存在多个java版本。例如, 〜$ whereis python - 告诉我多个位置 〜$ python - 给了我/ usr / bin / python

那么,你的IDE可以使用不同的python吗?一个,在终端中,找到路径但不在IDE中?