【发布时间】:2017-11-26 12:35:03
【问题描述】:
我想通过使用 php 运行带有 python3 版本 3.6.1 的脚本
$data = escapeshellarg($data);
$resultpy = shell_exec("/usr/local/bin/python3 path/to/myscript.py 2>&1 $data");
$data json 对象包含浮点数,我想在 python 中对其进行一些计算。我确实按照 Gordon Davisson 的建议 Cannot create a symlink inside of /usr/bin even as sudo 为 python3 设置了一个本地 bin,因为在 Mac OS X 10.9.5 上安装后 python3 没有出现在 /usr/bin/ 中。我对 python2 做了同样的事情来检查这在原则上是否可行。使用时
$resultpy = shell_exec("/usr/local/bin/python2 path/to/myscript.py 2>&1 $data");
,我可以运行python脚本并获得预期的输出,但在使用时却不行
$resultpy = shell_exec("/usr/local/bin/python3 path/to/myscript.py 2>&1 $data");
我也可以分别使用 /usr/local/bin/python3 或 /usr/local/bin/python2 在 shell 中成功调用 python3 和 python2。
python脚本本身会不会有问题(也许python3与python2相比有一些语法变化)?
from collections import OrderedDict
import sys, json
import scipy
import scipy.cluster.hierarchy as sch
try:
data = json.loads(sys.argv[1], object_pairs_hook=OrderedDict)
except (ValueError, TypeError, IndexError, KeyError) as e:
print json.dumps({'error': str(e)})
sys.exit(1)
print json.dumps(data)
我还在 python 脚本的顶部添加了一个 she-bang 行,但这并没有按预期解决它。所有导入模块都安装在两个版本中。非常感谢您提供任何帮助!
【问题讨论】:
-
将您打印的变量括在括号中(例如
print(....))
标签: php python json linux macos