我试图让python矩阵到php(努力解决这个问题超过20个小时)。我真的很感激你的帮助!
下面有两种情况:TEST和REAL。
它们具有完全相同的python运行输出以及相同的php代码。唯一的区别是,在REAL情况下,文件被提交到python脚本。 TEST python本身工作正常,REAL python本身工作正常,TEST php-python运行正常,但REAL php-python运行失败。所以我的猜测是php不允许python在文件中插入python脚本?我的代码如下:
===== test.py
import os, sys, json
g = json.loads( sys.argv[1] )
gene = ['>MhA1_Contig0.frz3.gene1', '>MhA1_Contig0.frz3.gene1', '>MhA1_Contig0.frz3.gene1']
gene.append(g)
seq = ["ATGGAC", "GGCACAGC", "CCACC"]
seq.append('ACTGAAA')
print json.dumps(output)
===== test.php
$output=null;
$g = "0.CUFF.6.1_2";
$data = escapeshellarg(json_encode($g));
exec("python /mysql/getSeq/test.py $data 2>&1 &", $output );
//exec("python /mysql/getSeq/test.py $data 2>&1", $output ); --> worked fine
//exec("python /mysql/getSeq/test.py $data", $output ); --> worked fine
//print_r($output); --> worked fine
//var_dump($output); --> worked fine
$result = json_decode( exec("python /mysql/getSeq/test.py $data", $output ) , true);
//print_r( $result); --> worked fine
//var_dump($result); --> worked fine
echo $result[0][0] . "<br>";
echo $result[0][1] . "<br>";
echo $result[1][0] . "<br>";
echo $result[1][1] . "<br>";
=====网页上的结果
>MhA1_Contig0.frz3.gene1
ATGGAC
>MhA1_Contig0.frz3.gene1
GGCACAGC
===== real.py
import os, sys, json
inF = file("getSeq_Mh.fasta", "r")
g = json.loads( sys.argv[1] )
inF = file("getSeq_Mh.fasta", "r")
line = inF.readline()
matrix = []
--- (some codes here) ---
inF = file("getSeq_Mh.fasta", "r")
line = inF.readline()
gene = []
--- (some codes here) ---
for i in range(len(index)) :
output[i][0] = ">" + gene[i]
output[i][1]= str(matrix[i])[2:-4]
print json.dumps(output)
# print output
===== real.php
$output=null;
$g = "0.CUFF.6.1_2";
$data = escapeshellarg(json_encode($g));
exec("python /mysql/getSeq/real.py $data 2>&1 &", $output );
//exec("python /mysql/getSeq/real.py $data 2>&1", $output ); --> no difference in results
//exec("python /mysql/getSeq/real.py $data", $output ); --> no difference in results
$result = json_decode( exec("python /mysql/getSeq/real.py $data 2>&1", $output ) , true);
print_r($output) . "<br>";
//var_dump($output) . "<br>";
//print_r( $result) . "<br>";
//var_dump($result) . "<br>";
//echo $result[0][0] . "<br>";
//echo $result[0][1] . "<br>";
//echo $result[1][0] . "<br>";
//echo $result[1][1] . "<br>";
=====网页上的结果
的print_r($输出); - &GT;错误
Array ( [0] => Traceback (most recent call last): [1] => File "/mysql/getSeq/getOne.py", line 3, in [2] => inF = file("getSeq_Mh.fasta", "r") [3] => IOError: [Errno 2] No such file or directory: 'getSeq_Mh.fasta' [4] => Traceback (most recent call last): [5] => File "/mysql/getSeq/real.py.py", line 3, in [6] => inF = file("getSeq_Mh.fasta", "r") [7] => IOError: [Errno 2] No such file or directory: 'getSeq_Mh.fasta' )
的var_dump($输出); - &GT;错误
array(8) { [0]=> string(34) "Traceback (most recent call last):" [1]=> string(95) " File "/mysql/getSeq/getOne.py", line 3, in " [2]=> string(38) " inF = file("getSeq_Mh.fasta", "r")" [3]=> string(63) "IOError: [Errno 2] No such file or directory: 'getSeq_Mh.fasta'" [4]=> string(34) "Traceback (most recent call last):" [5]=> string(95) " File "/mysql/getSeq/real.py.py", line 3, in " [6]=> string(38) " inF = file("getSeq_Mh.fasta", "r")" [7]=> string(63) "IOError: [Errno 2] No such file or directory: 'getSeq_Mh.fasta'" }
print_r($ result); - &GT;错误
(nothing)
的var_dump($结果); - &GT;错误
NULL
[['>MhA1_Contig0.frz3.gene1', 'ATGGACT'], ['>MhA1_Contig0.frz3.gene1', 'GGCACAGC'], ['>MhA1_Contig0.frz3.gene1', 'CCACC']]
答案 0 :(得分:0)
考虑使用PHP的proc_open()来处理子处理(输入/输出);这里的子进程是Python脚本。下面在命令行调用Python,传递$data
参数并接收$output
参数。
可能需要调整两个假设: python 是服务器上的路径变量,Python脚本与PHP脚本位于同一目录中。如果没有,请在命令行中插入 python.exe 的完整路径和/或在命令行中将目录的完整路径插入 real.py 。
$output=null;
$g = "0.CUFF.6.1_2";
$data = escapeshellarg(json_encode($g));
$cd = dirname(__FILE__); // CURRENT DIRECTORY OF SCRIPT
$output = null;
$descriptorspec = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
2 => array("file", $cd."/error.txt", "a") // ERROR TXT OUTPUT OF PROCESSING
);
/* CALLING COMMAND LINE */
$process = proc_open("python \"$cd/real.py\" \"$data\"" , $descriptorspec, $pipes, $cd);
/* RECEIVING OUTPUT */
$output = stream_get_contents($pipes[1]);
fclose($pipes[1]);
proc_close($process);
echo $output;
答案 1 :(得分:0)
它已经解决了。当我放入所有文件时(&#34; real.py&#34;,&#34; real.php&#34;和&#34; getSeq_Mh.fasta(已提交到real.py)&# 34;)在同一目录中,它工作。
谢谢,Parfait。你的推荐给了我一个暗示。