I had a similar problem described in the " .cgi problem with the web server " section , although I unsuccessfully reviewed and tested the previously proposed solutions.
I am running the same program on Mac OS X 10.5.8, Apache 2.2.13 using Python 2.6.4. I can successfully run the code in the python shell and on the command line of the terminal, but I get <type 'exceptions.ImportError'>: No module named MySQLdbit when I try to run it on " http: //localhost/cgi-bin/test.cgi ". It starts successfully if I comment import MySQLdb.
import cgitb
cgitb.enable()
import MySQLdb
print "Content-Type: text/html"
print
print "<html><head><title>Books</title></head>"
print "<body>"
print "<h1>Books</h1>"
print "<ul>"
connection = MySQLdb.connect(user='me', passwd='letmein', db='my_db')
cursor = connection.cursor()
cursor.execute("SELECT name FROM books ORDER BY pub_date DESC LIMIT 10")
for row in cursor.fetchall():
print "<li>%s</li>" % row[0]
print "</ul>"
print "</body></html>"
connection.close()
[edit] Based on the first answer:
test.cgi , MySQLdb sys.path. , -, . import MySQLdb test.cgi for, .
Apache PYTHONPATH? python :
import MySQLdb
import os
print os.path.dirname(MySQLdb.__file__)
, , test.cgi:
import sys
sys.path.append('/Library/Frameworks/Python.framework/Versions/6.0.0/lib/python2.6/site-packages/MySQL_python-1.2.3c1-py2.6-macosx-10.5-i386.egg/')
.
[]
, . sys.path , . - python #!/Library/Frameworks/Python.framework/Versions/Current/bin/python , :
A problem occurred in a Python script. Here is the sequence of function calls leading up to the error, in the order they occurred.
/Library/WebServer/CGI-Executables/test.cgi in ()
15
16
17 import MySQLdb
18
19
MySQLdb undefined
/Library/WebServer/CGI-Executables/build/bdist.macosx-10.5-i386/egg/MySQLdb/__init__.py in ()
/Library/WebServer/CGI-Executables/build/bdist.macosx-10.5-i386/egg/_mysql.py in ()
/Library/WebServer/CGI-Executables/build/bdist.macosx-10.5-i386/egg/_mysql.py in __bootstrap__()
/Library/Frameworks/Python.framework/Versions/6.0.0/lib/python2.6/site-packages/pkg_resources.py in resource_filename(self=<pkg_resources.ResourceManager instance at 0x3c8a80>, package_or_requirement='_mysql', resource_name='_mysql.so')
848 """Return a true filesystem path for specified resource"""
849 return get_provider(package_or_requirement).get_resource_filename(
850 self, resource_name
851 )
852
self = <pkg_resources.ResourceManager instance at 0x3c8a80>, resource_name = '_mysql.so'
...
<class 'pkg_resources.ExtractionError'>: Can't extract file(s) to egg cache The following error occurred while trying to extract file(s) to the Python egg cache: [Errno 13] Permission denied: '/Library/WebServer/.python-eggs' The Python egg cache directory is currently set to: /Library/WebServer/.python-eggs Perhaps your account does not have write access to this directory? You can change the cache directory by setting the PYTHON_EGG_CACHE environment variable to point to an accessible directory.
args = ("Can't extract file(s) to egg cache\n\nThe followin...nt\nvariable to point to an accessible directory.\n",)
cache_path = '/Library/WebServer/.python-eggs'
manager = <pkg_resources.ResourceManager instance at 0x3c8a80>
message = "Can't extract file(s) to egg cache\n\nThe followin...nt\nvariable to point to an accessible directory.\n"
original_error = OSError(13, 'Permission denied')
, , , - PYTHON_EGG_CACHE / ...
[edit] :
, Python Apache, :
import sys
version = sys.version
path = sys.path
...
print "<h1>%s</h1>" % version
print "<h1>%s</h1>" % path
, Apache Python 2.5.1, Python 2.6.4 MySQLdb. , test.cgi :
import os
os.environ['PYTHON_EGG_CACHE'] = '/tmp'
import sys
sys.path.append('/Library/Frameworks/Python.framework/Versions/6.0.0/lib/python2.6/site-packages/MySQL_python-1.2.3c1-py2.6-macosx-10.5-i386.egg')
, , PYTHONPATH APACHE httpd.conf, .