无法通过文件加载 Python 包

Unable to load a Python package via a file

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

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()

这是我的 graphite.conf for 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

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

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

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

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

中找不到的路径

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

您想使用哪个 Python 版本以及您的用途是什么:

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

当 运行 来自口译员时。

然后算出 Python mod_wsgi 使用的是什么版本:


更新 1

检查 django 模块通常来自命令行 Python,这样您就可以查看您是否使用与 mod_wsgi 相同的 Python 安装编译并使用,你会在解释器中做:

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

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

所以django模块必须有一个。