python加载包出错_无法通过文件加载Python包

I am trying to load a file having these contents:

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

I keep getting this error:

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

When I bring up Python on the command line and try to load this module as follows, I get no error:

import django.core.handlers.wsgi

I checked the permissions for django and all its subdirectories. Any ideas?

These are the errors in 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

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

this is my graphite.conf for apache:

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

Listen 8090

LoadModule wsgi_module /usr/lib64/apache2/mod_wsgi.so

WSGISocketPrefix /etc/apache2/wsgi

Options All

AllowOverride All

Order deny,allow

Allow from all

DocumentRoot "/opt/graphite/webapp"

#

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/

SetHandler None

#

Alias /media/ "/usr/local/lib64/python2.6/site-packages/django/contrib/admin/media/"

#

SetHandler None

Allow from all

#

LogLevel debug

ErrorLog /var/log/apache2/graphite_error

It keeps giving me error about this file:

/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'.

when I am on the shell and issue:

python /opt/graphite/conf/graphite.wsgi

I get no error. Not sure what the problem here?

解决方案

Your problem is likely going to be because mod_wsgi is compiled for and bound to a different Python version/installation than you intend to use. It therefore isn't looking at the spot where you installed all your packages.

What Python version are you wanting to use and what do you get for:

import sys

print(sys.version_info)

print(sys.prefix)

when running that from the interpreter.

Then work out what version of Python mod_wsgi is using:

UPDATE 1

To check where the django module is coming from normally from command line Python, so you can see whether you are using the same Python installation as mod_wsgi is compiled for and using, you would in the interpreter do:

>>> import django

>>> print django.__file__

/some/path/lib/python2.7/site-packages/django/__init__.pyc

Modules should have the __file__ attribute, with the exception of builtin modules which are static linked into the Python binary.

So the django module must have one.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值