500 internal server error when importing a python module in wsgi

≯℡__Kan透↙ 提交于 2019-12-06 08:42:47

Apache's 500 error just tells you that the Python script is returning an error. My guess is that in the Apache environment, Python can't find that module. Try a simple script that prints sys.path and makes sure it has the directories in it you expect.

Adding

WSGIPythonPath /path/to/my/py/files/folder

before any other alias or directory in the application conf file really makes the difference. Problem solved!

Maybe you should use this pattern for cgi scripts to get errors to html form:

import cgi
import cgitb; cgitb.enable()  # for troubleshooting
import traceback
# code here
except:
    tb = traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback)
    tb = ''.join(tb)
    print '<pre>%s</pre></body></html>' % tb

If an exception propagates back up to mod_wsgi then the details should be logged to appropriate Apache error log. If it isn't, then it may be that the WSGI framework/toolkit you are using is capturing the exception and returning a generic HTTP 500 response page. If that is the case, you may need to enable debug option in that WSGI framework/toolkit being used.

Alternatively, the problem isn't with the WSGI script file/application but with Apache configuration in which case you wouldn't be looking for a Python exception/traceback. You thus need to look more carefully at the Apache error log files and look for any error at all and provide what it says.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!