Python导入MySQLdb,Apache内部服务器错误

发布于 2024-08-20 20:03:05 字数 4031 浏览 3 评论 0原文

我遇到了与“.cgi Web 服务器问题”中描述的类似问题”,尽管我审查并测试了之前建议的解决方案,但没有成功。

我在 Mac OS X 10.5.8、Apache 2.2.13 上使用 Python 2.6.4 运行相同的程序。我可以在 python shell 和终端命令行中成功运行代码,但当我尝试在 " 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()

[编辑]基于第一个答案:

如果我按指定修改test.cgi并从终端命令行运行它,则MySQLdb的目录code> 显示在 sys.path 中。然而,当我通过网络服务器运行它时,我得到了同样的错误。如果我使用新的 for 循环注释掉 test.cgi 中的 import MySQLdb,则页面无法打开。

如何设置 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/')

但这产生了相同的错误。


[编辑]

不幸的是,这两种解决方案都不起作用。将路径添加到 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 和/或权限有关...

I'm having a similar problem to that described in ".cgi problem with web server", although I reviewed and tested the previously suggested solutions without success.

I'm 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 the terminal command-line, but I get <type 'exceptions.ImportError'>: No module named MySQLdb when I try to run it at "http://localhost/cgi-bin/test.cgi". It successfully runs if I comment out 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] Based on the first answer:

If I modify test.cgi as specified and run it from the terminal command-line, the directory of MySQLdb is shown in sys.path. However, when I run it via the web server, I get the same error. If I comment out import MySQLdb in test.cgi with the new for-loop, the page fails to open.

How do I set Apache's PYTHONPATH? At the python shell, I tried:

import MySQLdb
import os
print os.path.dirname(MySQLdb.__file__)

Then, based on other posts, I tried to add the resultant path in the original 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/')

but this produced the same error.


[edit]

Unfortunately, neither solution worked. Adding the path to sys.path gave me the same error as before. Hard-coding the path to the python binary #!/Library/Frameworks/Python.framework/Versions/Current/bin/python produced a lengthy error, part of which is shown:

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')

This error seems to imply that it may have something to do with setting PYTHON_EGG_CACHE and/or permissions...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鹤仙姿 2024-08-27 20:03:05

确保 Apache 使用的 Python 二进制文件的 PYTHONPATH 中包含 MySQLdb

通过检查工作脚本中 MySQLdb 的位置来确认这一点:

import os
print os.path.dirname(MySQLdb.__file__)

并将其与 test.cgi 内的 sys.path 的输出进行比较:

import sys
for p in sys.path: 
    print p + "<br/>"

如果 的目录工作脚本中的 MySQLdb 不在损坏的脚本上的 sys.path 中,这就是您的问题。

编辑:

根据OP的更新,您需要添加到PYTHONPATH的目录似乎是/Library/Frameworks/Python.framework/版本/6.0.0/lib/python2.6/site-packages/。您使用的另一个路径:

/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/

... 是安装的 .egg 的位置。由于这个原因,我讨厌 .egg 安装,因为它可能会让人困惑,特别是对于那些对 Python 比较陌生的人来说。

另一种选择是将 test.cgi 中的 Python 二进制文件的路径硬编码为您已确认从命令行使用的路径。在您的脚本中,您有 /usr/bin/env python,这是一个很好的做法。然而,当您遇到像您这样的环境和路径问题时,最好对 python 二进制文件进行硬编码,直到您能够克服这个障碍。

从命令行执行 which python,以确定 CLI 正在引用哪个 python 二进制文件:

% which python
/opt/local/bin/python 

我的 python 二进制文件的路径是 /opt/local/bin/python。因此,如果这是您的,请将 /usr/bin/env python 替换为 test.cgi 中的内容:

#!/opt/local/bin/python
import cgitb
# ... and so on ...

执行此操作后,您是否仍然收到 ImportError代码>?如果是这样,您就缩小了问题范围。将 /Library/Frameworks/Python.framework/Versions/6.0.0/lib/python2.6/site-packages/ 添加到 sys.path 可能会解决您的问题,而无需硬编码 python 二进制文件的路径。

Make sure the python binary that Apache is using has MySQLdb in its PYTHONPATH.

Confirm it by checking the location of MySQLdb in the working script:

import os
print os.path.dirname(MySQLdb.__file__)

And comparing that to the output of sys.path inside of test.cgi:

import sys
for p in sys.path: 
    print p + "<br/>"

If the directory of MySQLdb from the working scripting is not in sys.path on the broken script, there is your problem.

Edit:

Based on the update to the OP, it looks like the directory that you need to add to your PYTHONPATH is /Library/Frameworks/Python.framework/Versions/6.0.0/lib/python2.6/site-packages/. The other path you used:

/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/

... is the location of the .egg that was installed. I hate .egg installs for this reason because it can be confusing, especially to people who are relatively new to Python.

The other option is to hard-code the path to the Python binary in your test.cgi to the same one you have confirmed you are using from the command-line. In your script you have /usr/bin/env python, which is a good practice. However, when you're running into environment and path problems like you are, it might be a good idea to hard-code the python binary until you can get past this hurdle.

From the command-line perform a which python, to determine which python binary the CLI is referencing:

% which python
/opt/local/bin/python 

The path to my python binary is /opt/local/bin/python. So if that were yours, replace /usr/bin/env python with that in test.cgi:

#!/opt/local/bin/python
import cgitb
# ... and so on ...

After doing that, are you still receiving the ImportError? If so, you've narrowed down the problem. Adding /Library/Frameworks/Python.framework/Versions/6.0.0/lib/python2.6/site-packages/ to sys.path may solve your problem without having to hard-code the path to the python binary.

爱要勇敢去追 2024-08-27 20:03:05

我也遇到这个问题,但是经过几个小时的尝试才解决。

这是我的场景:我需要在 Python cgi 文件中导入 MySQLdb,但它失败了,并引发了ImportError

然后我打印出Web服务器路径和本地python路径,它们是:

Web服务器路径

/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC

/Library/Python/2.7/site-packages

和 Python 路径:

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/distribute-0.6.25-py2.7.egg
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pydelicious-0.6-py2.7.egg
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/feedparser-5.1.2-py2.7.egg
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/googlemaps-1.0.2-py2.7.egg
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/MySQL_python-1.2.3-py2.7-macosx-10.5-intel.egg
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/elementtree-1.2.7_20070827_preview-py2.7.egg
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/oauth2-1.5.170-py2.7.egg
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/httplib2-0.7.4-py2.7.egg
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/simplejson-2.1.6-py2.7-macosx-10.5-intel.egg
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/python_twitter-0.8.3-py2.7.egg
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/South-0.7.6-py2.7.egg
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pyfb-0.4.1-py2.7.egg
/usr/local/lib/wxPython-ansi-2.8.12.1/lib/python2.7/site-packages
/usr/local/lib/wxPython-ansi-2.8.12.1/lib/python2.7/site-packages/wx-2.8-mac-ansi
/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/PIL
/usr/local/lib/wxPython-ansi-2.8.12.1/lib/python2.7

/Library/Python/2.7/site-packages

它们都有路径 /Library/Python/2.7/site-packages ,显然,这是安装第三方包的路径,但它是空的。

为什么?

我可以在带有附加软件包的本地Python环境中很好地运行Python脚本,然后我注意到所有这些软件包都安装在

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/

当我在Web服务器上运行cgi文件时,服务器找不到上述路径,当然导入 MySQLdb 失败。

我在这里所做的是将文件复制到其中

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/"

并且

/Library/Python/2.7/site-packages

它可以工作。

I have this problem too, but I figured it out after several hours trying.

Here is my scenario: I need to import MySQLdb in a Python cgi file, but it failed, and raised the ImportError.

Then I print out the Web server path and local python path, they were:

Web Server path

/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC

/Library/Python/2.7/site-packages

and Python path:

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/distribute-0.6.25-py2.7.egg
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pydelicious-0.6-py2.7.egg
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/feedparser-5.1.2-py2.7.egg
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/googlemaps-1.0.2-py2.7.egg
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/MySQL_python-1.2.3-py2.7-macosx-10.5-intel.egg
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/elementtree-1.2.7_20070827_preview-py2.7.egg
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/oauth2-1.5.170-py2.7.egg
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/httplib2-0.7.4-py2.7.egg
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/simplejson-2.1.6-py2.7-macosx-10.5-intel.egg
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/python_twitter-0.8.3-py2.7.egg
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/South-0.7.6-py2.7.egg
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pyfb-0.4.1-py2.7.egg
/usr/local/lib/wxPython-ansi-2.8.12.1/lib/python2.7/site-packages
/usr/local/lib/wxPython-ansi-2.8.12.1/lib/python2.7/site-packages/wx-2.8-mac-ansi
/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/PIL
/usr/local/lib/wxPython-ansi-2.8.12.1/lib/python2.7

/Library/Python/2.7/site-packages

They both have the path /Library/Python/2.7/site-packages, obviously, this is a path for installing third-party packages, but it is empty.

Why?

I can run Python script very well on local python environment with additional packages, then I noticed that all those packages were installed at

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/

When I run cgi file on web server, the above path can not be found by the server, and of course import MySQLdb failed.

What I did here is to copy the files in

/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/"

to

/Library/Python/2.7/site-packages

and it works.

凤舞天涯 2024-08-27 20:03:05

为了测试 Apache 使用的 Python 版本,我添加了以下代码:

import sys
version = sys.version
path = sys.path

...

print "<h1>%s</h1>" % version                                                                  
print "<h1>%s</h1>" % path

这表明 Apache 确实使用制造商安装的 Python 2.5.1,而不是带有关联 MySQLdb 模块的 Python 2.6.4。因此,将以下代码添加到原始 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')

通过更改 APACHE 的 httpd.conf 中的 PYTHONPATH 可能有系统性修复,但我还没有弄清楚。


此答案是作为问题编辑发布的questions/2232542/python-import-mysqldb-apache-internal-server-error">Python 导入 MySQLdb,Apache 内部服务器错误 由OP bernie 根据 CC BY-SA 2.5。

To test which version of Python that Apache was using, I added the following code:

import sys
version = sys.version
path = sys.path

...

print "<h1>%s</h1>" % version                                                                  
print "<h1>%s</h1>" % path

which indicated that Apache was indeed using the manufacturer-installed Python 2.5.1, and not Python 2.6.4 with the associated MySQLdb module. Thus, adding the following code to the original test.cgi fixed the problem:

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')

There is possibly a systematic fix by altering the PYTHONPATH in APACHE's httpd.conf, but I haven't yet figured it out.


This answer was posted as an edit to the question Python import MySQLdb, Apache Internal Server Error by the OP bernie under CC BY-SA 2.5.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文