dopq87915 2011-05-18 22:17
浏览 94
已采纳

通过终端和PHP调用python脚本之间的区别? 错误在哪里?

I have a PHP script, that calls a python script by

$call_python = "python ../python/lp_3.py ".$author;
$python_output = Null;
$mystring = exec($call_python, $output_python);

This produces me an error in the log:

$ vi logs/error_log shows
....
Traceback (most recent call last):
    File "../python/lp_3.py", line 14, in <module>
        import MySQLdb
ImportError: No module named MySQLdb

If I do python python/lp_3.py in the terminal everything is fine. What do I miss?

Edit:

After the suggestion of @S.Lott I had a look at the variables PATH and PYTHONPATH both in the terminal and in PHP.

In the terminal:

$ echo $PYTHONPATH

$ echo $PATH
/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/git/bin:/usr/texbin:/usr/X11/bin

As you can see, PYTHONPATH is empty.

In PHP:

echo getenv("PYTHONPATH"); // NOTHING
echo getenv("PATH"); // /usr/bin:/bin:/usr/sbin:/sbin

Perhaps I should mention that the first two lines in my python script are

#!/usr/bin/env python
# encoding: utf-8

I am open for suggestions. =)

Edit2:

I checked every installed python version on my mac. I found out, that python2.7 has no MySQLdb installed. Is there a way to tell PHP not to use python2.7 and to use e.g. python2.6 instead? I tryed toying with setenv() in PHP but I couldn't figure out how to use it properly, and I don't even know if this is the right approach.

  • 写回答

2条回答 默认 最新

  • doudiewen9435 2011-05-19 01:22
    关注

    In your PHP code, you're just calling "python", and letting PHP decide which version of Python to use. Use an explicit path to a specific Python binary, (e.g. /usr/bin/python2.6).

    You need to know the exact path to the version of Python that has MySQLdb installed.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退
  • ¥20 win系统的PYQT程序生成的数据如何放入云服务器阿里云window版?