Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用shell_exec从php的usr/local/bin执行python3_Php_Python_Json_Linux_Macos - Fatal编程技术网

使用shell_exec从php的usr/local/bin执行python3

使用shell_exec从php的usr/local/bin执行python3,php,python,json,linux,macos,Php,Python,Json,Linux,Macos,我想使用 $data = escapeshellarg($data); $resultpy = shell_exec("/usr/local/bin/python3 path/to/myscript.py 2>&1 $data"); $data json对象包含浮点数,我想用python对其进行一些计算。我确实按照Gordon Davidson的建议为python3设置了一个本地bin,因为在Mac OS X 10.9.5上安装后,python3不会出现在/usr/bin/中

我想使用

 $data = escapeshellarg($data);
 $resultpy = shell_exec("/usr/local/bin/python3 path/to/myscript.py 2>&1 $data");
$data json对象包含浮点数,我想用python对其进行一些计算。我确实按照Gordon Davidson的建议为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"); 
$resultpy = shell_exec("/usr/local/bin/python3 path/to/myscript.py 2>&1 $data"); 
,我可以运行python脚本并获得预期的输出,但在使用

$resultpy = shell_exec("/usr/local/bin/python2 path/to/myscript.py 2>&1 $data"); 
$resultpy = shell_exec("/usr/local/bin/python3 path/to/myscript.py 2>&1 $data"); 
我还可以在shell中分别使用/usr/local/bin/python3或/usr/local/bin/python2成功地调用python3和python2

python脚本本身可能有什么问题(可能是python3与python2相比的一些语法变化)


我还在python脚本的顶部添加了she-bang行,但这并没有像预期的那样解决它。所有导入模块都安装在两个版本中。非常感谢您的帮助

如果你想让它在python3中工作,你需要用print更改行

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) )

将打印的变量括在括号中(例如,
print(…)