[英]500 internal server error when importing a python module in wsgi
I've got a Python script that is executing functions asynchronously by using PEST wsgi library. 我有一个Python脚本,该脚本通过使用PEST wsgi库异步执行功能。 However, when I try to import another module it simply results in a 500 error.
但是,当我尝试导入另一个模块时,它只会导致500错误。
The way I try to reference it is: 我尝试引用它的方式是:
from foo import *
from foo import Foo
where foo is a file .py in which I have the object that I want to reference to. 其中foo是文件.py,其中包含我要引用的对象。
Tried to monitor the calls through Chrome's Inspect Element Control but couldn't find a thing. 试图通过Chrome的Inspect Element Control监视呼叫,但找不到任何东西。
Also tried to debug using Apache's error log, but nothing there. 还尝试使用Apache的错误日志进行调试,但没有任何帮助。
Any hints appreciated. 任何提示表示赞赏。
Update: I've tried the following which resulted in the same 500 error: 更新:我尝试了以下导致相同的500错误:
--make use of - 利用
import site
and 和
site.addsitedir("path/to/my/py/files/folder")
--modify the Apache2 httpd.conf file by inserting the following: -通过插入以下内容来修改Apache2 httpd.conf文件:
WSGIPythonPath /path/to/my/py/files/folder
--modify the application conf file in /etc/apache2/sites-available/myapp.conf, by inserting the above WSGIPythonPath -通过插入以上WSGIPythonPath来修改/etc/apache2/sites-available/myapp.conf中的应用程序conf文件
Apache's 500 error just tells you that the Python script is returning an error. Apache的500错误只是告诉您Python脚本正在返回错误。 My guess is that in the Apache environment, Python can't find that module.
我的猜测是在Apache环境中,Python无法找到该模块。 Try a simple script that prints
sys.path
and makes sure it has the directories in it you expect. 尝试一个简单的脚本,该脚本会打印
sys.path
并确保它包含您期望的目录。
Adding 添加
WSGIPythonPath /path/to/my/py/files/folder
before any other alias or directory in the application conf file really makes the difference. 在应用程序conf文件中的任何其他别名或目录真正起作用之前。 Problem solved!
问题解决了!
Maybe you should use this pattern for cgi scripts to get errors to html form: 也许您应该对cgi脚本使用此模式,以获取HTML表单错误:
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. 如果异常传播回了mod_wsgi,则应将详细信息记录到适当的Apache错误日志中。 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.
如果不是,则可能是您使用的WSGI框架/工具包正在捕获异常并返回通用的HTTP 500响应页面。 If that is the case, you may need to enable debug option in that WSGI framework/toolkit being used.
如果是这样,您可能需要在使用的WSGI框架/工具包中启用调试选项。
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. 另外,问题不在于WSGI脚本文件/应用程序,而在于Apache配置,在这种情况下,您将不需要查找Python异常/回溯。 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.
因此,您需要更仔细地查看Apache错误日志文件,并查找所有错误并提供错误内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.