如何让MySQLdb在OSX上与www用户一起工作?

2024-04-19 14:07:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我刚刚设法在OSX上安装了mySQLdb for Python,从命令行来看,它运行得很好。我可以查询数据库没问题。在

我现在要做的是使用system()函数从PHP调用一个python脚本。在

此片段:

import MySQLdb

print "hello from py"

当runfrom PHP system()返回“1”到PHP,但是如果我从命令行运行它,它会输出“hello from py”。如果我注释掉import语句,并使用system()再次通过PHP运行脚本,则返回“hello from py”

因此,我确信这一定与MySQLdb有关。我不太擅长UNIX,但我猜这可能与用户通过PHP访问mySQLdb模块的方式不正确有关??在

我不能让它返回任何错误到PHP,所以很难排除问题。在

如果有人能帮我指出正确的方向,我将不胜感激:将Python的错误消息传递给PHP,并自行解决问题。在

编辑:

一些更好的信息:

我把片段改成这样:

^{pr2}$

并使用PHP exec()而不是system执行它:

exec('python /Users/Tapefreak/sites/a/testpy.py');

这就是PHP的成果:

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.

所以,我创建了一个目录并把它修改了。在

然后,脚本的输出更改为:

dlopen(/Library/WebServer/.python-eggs/MySQL_python-1.2.3b2-py2.6-macosx-10.6-universal.egg-tmp/_mysql.so, 2): Library not loaded: libmysqlclient.18.dylib Referenced from: /Library/WebServer/.python-eggs/MySQL_python-1.2.3b2-py2.6-macosx-10.6-universal.egg-tmp/_mysql.so Reason: image not found

这是我之前收到的一条消息,但我原以为是固定的——结果它只针对我的本地用户,而不是针对www,也不是针对root用户。(忍着,我在学……)

再深入一点,我发现这个解决方案可以添加一个符号链接:Python mysqldb: Library not loaded: libmysqlclient.18.dylib

我在/usr/lib中添加了:

ln -s /usr/local/mysql-5.5.16-osx10.6-x86_64/lib/libmysqlclient.18.dylib libmysqlclient.18.dylib

现在它似乎可以工作了:PHP可以使用MySQLdb执行Python。在


Tags: tofrompy脚本cacheegglibrarynot
2条回答

请检查apache错误日志中的错误。 另外,尝试访问www并手动执行脚本。在

如上所述,将/usr/lib中的符号链接添加到/usr/local/mysql-5.5.16-osx10.6-x86_64/lib/libmysqlclient.18.dylib中,为我解决了这个问题。希望这对其他人有用。在

相关问题 更多 >