[英]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()
这是我的Apache的石墨.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'.
当我在外壳上并发出:
python /opt/graphite/conf/graphite.wsgi
我没有错。 不知道这里出了什么问题?
您的问题很可能是因为mod_wsgi的编译版本和绑定到的Python版本/安装与您打算使用的版本不同。 因此,它不是在寻找安装所有软件包的位置。
您要使用什么Python版本以及获得什么?
import sys
print(sys.version_info)
print(sys.prefix)
从解释器运行时。
然后找出正在使用哪个版本的Python mod_wsgi:
更新1
要检查django
模块通常来自命令行Python的位置,以便可以查看您是否正在使用与为mod_wsgi编译和使用的Python安装相同的Python安装,您可以在解释器中执行以下操作:
>>> import django
>>> print django.__file__
/some/path/lib/python2.7/site-packages/django/__init__.pyc
模块应具有__file__
属性,但内置模块除外,这些模块静态链接到Python二进制文件中。
因此, django
模块必须具有一个。
您可能/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
的import django.core.handlers.wsgi
工作正常。 但这不是错误所在的位置。 实际的错误是,无论您使用什么,都无法将文件/opt/graphite/conf/graphite.wsgi
加载为python模块,而不是任何django模块。
我是Python的新手,但是最近与Netbeans进行了一次斗争,试图弄清为什么Java模块加载错误,因此我了解到Ubuntu 14.04机器上存在多个Java版本。 例如,〜$ whereis python-告诉我多个位置〜$哪个python-给了我/ usr / bin / python
因此,您的IDE可以使用其他python吗? 在终端中找到路径但在IDE中找不到路径的那个?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.