导入使 Python-CGI 崩溃(错误 500)

Import makes Python-CGI crash (error 500)

script1.py:

import cgi
import cgitb

print("Content-type:text/html\r\n\r\n")
print("hello")

和script2.py:

import cgi
import cgitb
import pymysql    

print("Content-type:text/html\r\n\r\n")
print("hello")

script1.py 中一切正常(打印 'hello'),但是 script2.py returns 500 错误。

注意事项:

那么,为什么script2.py不行呢?

您可能会考虑您的脚本是否正在 运行 网络服务器(而不是您的用户 shell 会话)在不同的虚拟环境中,可能是没有安装 pymysql 的环境.如果是这种情况,您需要激活该虚拟环境并在那里安装 pymysql。

要验证这是一个未安装的库实例,您可以在脚本中尝试类似的操作...

import cgi
import cgitb
import sys

print("Content-type:text/html\r\n\r\n")
print("hello")

if 'pymysql' in sys.modules:
   print 'pymysql is installed'
else:
   print 'pymysql is NOT installed'

除此之外,检查您的 Web 服务器日志可能会更深入地了解 Web 服务器引发 500 错误的原因...