我遇到了与“中描述的类似问题”Web 服务器的 .cgi 问题 https://stackoverflow.com/questions/621874/cgi-problem-with-web-server”,尽管我审查并测试了之前建议的解决方案,但没有成功。
我在 Mac OS X 10.5.8、Apache 2.2.13 上使用 Python 2.6.4 运行相同的程序。我可以在 python shell 和终端命令行中成功运行代码,但我得到<type 'exceptions.ImportError'>: No module named MySQLdb
当我尝试在“http://localhost/cgi-bin/test.cgi”运行它时。如果我注释掉它会成功运行import MySQLdb
.
#!/usr/bin/env python
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]基于第一个答案:
如果我修改test.cgi
按照指定并从终端命令行运行它,目录MySQLdb
显示在sys.path
。然而,当我通过网络服务器运行它时,我得到了同样的错误。如果我评论掉import MySQLdb
in test.cgi
使用新的 for 循环,页面无法打开。
如何设置 Apache 的 PYTHONPATH?在 python shell 中,我尝试过:
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/')
但这产生了同样的错误。
[edit]
不幸的是,这两种解决方案都不起作用。添加路径到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 #sys.path.append('/Library/Frameworks/Python.framework/Versions/6.0.0/lib/python2.6/site-packages/')
16
17 import MySQLdb
18 #import _mysql
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 和/或权限有关...