简体   繁体   English

在wsgi中导入python模块时500内部服务器错误

[英]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.

相关问题 WSGI脚本无法作为Python模块加载— 500内部服务器错误 - WSGI Script Cannot Be Loaded as Python Module — 500 Internal Server Error Python WSGI + Flask render_template-500内部服务器错误? - Python WSGI + Flask render_template - 500 Internal Server Error? 为什么在导入本地 python 文件时出现 500 内部服务器错误? - Why am I getting a 500 Internal Server Error when importing local python file? 500 内部服务器错误 mod_wsgi apache &quot;importerror: No Module named &#39;django&#39; - 500 internal server error mod_wsgi apache "importerror: No Module named 'django' Python 2.7和Flask-使用venv时,使用“ Random”模块中的函数返回“ 500 Internal Server Error” - Python 2.7 and Flask - using functions from “Random” module returns “500 Internal Server Error” when using venv 在生产服务器上部署期间,我在 django 应用程序中收到 500 内部服务器错误 ModuleNotFoundError: No module named &#39;main&#39; (wsgi:error) - I am getting 500 Internal Server Error in django app during deloying on production server ModuleNotFoundError: No module named 'main' (wsgi:error) 配置 python cgi 时出现 500 内部服务器错误 - 500 internal Server error when configuring python cgi 500内部服务器错误:Python Bottle API - 500 Internal Server Error: Python Bottle API Python CGI收到500内部服务器错误 - Python CGI getting 500 Internal Server Error Python CGI显示500内部服务器错误 - Python CGI Showing 500 internal server error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM