简体   繁体   中英

Import makes Python-CGI crash (error 500)

Being script1.py:

import cgi
import cgitb

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

and script2.py:

import cgi
import cgitb
import pymysql    

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

In script1.py everything works fine (prints 'hello'), but script2.py returns a 500 error.

Considerations:

  • pymysql is installed and the proof is that it can be imported from the python interactive shell without trhowing any exception
  • both files have the same permissions

Then, why script2.py doesn't work?

You might consider if your script is being run by the web server (as opposed to your user shell session) in a different virtual environment, possibly one that doesn't have pymysql installed. If that's the case, you'll need to activate that virtual environment and install pymysql there.

To verify this is an instance of the library not being installed, you might try something like this in your script...

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'

Beyond this, checking your web server logs might provide more insight into why the 500 error is getting raised by the web server...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM