普通网友 2016-07-26 11:53
浏览 419
已采纳

PHP exec命令运行python脚本但不运行python3

I currently have python 2.7 installed as part of OSX, and recently installed 3.5.2.

I'm running a local webserver on my mac using XAMPP, and when I execute the python script from within apache, it loads fine:

$executePython = "python " . __DIR__ . "/cycle/cutoff.py $device_id $processPreviousMinutes";
exec("$executePython");

However, when I replace python with python3 my script refuses to run. I can invoke it manually from the command line using both versions, however it seems like the apache account/daemon doesn't have access to python3. Would this be something to do with a configuration file that I've overlooked?

  • 写回答

1条回答 默认 最新

  • dou8mwz5079 2016-07-27 10:51
    关注

    We don't want to mess up with the system wide path on the latest OSX. What if you add the python3 path in your script like this and then do your normal stuff

    putenv("PATH=/usr/local/bin/:" . exec('echo $PATH'));
    $executePython = "python3 " . __DIR__ . "/cycle/cutoff.py $device_id $processPreviousMinutes";
    exec("$executePython");
    

    putenv just adds your python3 path to the whatever current path is in your XAMPP's apache.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?