问题
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 <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()
this is my 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>
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?
回答1:
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:
- https://code.google.com/p/modwsgi/wiki/CheckingYourInstallation#Python_Shared_Library
- https://code.google.com/p/modwsgi/wiki/CheckingYourInstallation#Python_Installation_In_Use
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.
回答2:
Your are probably loading the /opt/graphite/conf/graphite.wsgi
through a webserver or something. Can you show us what you do when you get this error.
Have you checked /opt/graphite/conf/graphite.wsgi
is executable ?
chmod +x /opt/graphite/conf/graphite.wsgi
The import of import django.core.handlers.wsgi
is working fine. But thats not where the error is located. The actual error is, that whatever you are using cannot load the file /opt/graphite/conf/graphite.wsgi
as python module not any django module.
回答3:
Being new to Python but having recently done battle with Netbeans trying to figure out why a java module was loading wrong, I learned that multiple java versions exist on my Ubuntu 14.04 box. For example, ~$ whereis python -- told me of multiple locations ~$ which python -- gave me /usr/bin/python
So, could your IDE be using a different python? One that, in the terminal, finds the path but not in the IDE?
来源:https://stackoverflow.com/questions/32125658/unable-to-load-a-python-package-via-a-file