dopq87915 2011-05-18 14: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-18 17: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条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥30 stm32f103c8t6制作万能红外遥控器
  • ¥15 有人会fastcrud写前端页面吗
  • ¥15 如何解除Uniaccess管控
  • ¥15 微信小程序跳转关联公众号
  • ¥15 Java AES 算法 加密采用24位向量报错如何处理?
  • ¥15 使用X11可以找到托盘句柄,监控到窗口点击事件但是如何在监听的同时获取托盘中应用的上下文菜单句柄
  • ¥45 字符串操作——数组越界问题
  • ¥15 Loss下降到0.08时不在下降调整学习率也没用
  • ¥15 QT+FFmpeg使用GPU加速解码
  • ¥15 为什么投影机用酷喵播放电影放一段时间就播放不下去了?提示发生未知故障,有什么解决办法吗?
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部