title: how to "php echo from python print"?
body: <p>I am trying to get python matrix to php (struggling for resolving this problem for more than 20 hours). I would really appreciate for any help!</p>
  
  <p>There are two cases below: TEST and REAL. </p>
  
  <ul>
  <li>TEST: A variable of php is passed to python. With the passed variable into python, a matrix value is explicitly assigned. This matrix should be called to php. </li>
  <li>REAL: A variable of php is passed to python. Along with the variable, one file is in-filed to python. Then, parsed value is assigned to matrix which should be called to php. </li>
  </ul>
  
  <p>They have the exactly same output of python running as well as the same php code. The only difference is that in the REAL case, a file is in-filed to python script. TEST python itself works fine, REAL python itself works fine, TEST php-python running works fine, but REAL php-python running fails. So my guess is that php does not allow python to in-file into the python scripts?? My codes look like:</p>
  
  <h1>=========== TEST CASE ===========</h1>
  
  <p>===== test.py </p>
  
  <pre><code>import os, sys, json
  g = json.loads( sys.argv[1] )
  
  gene = ['&gt;MhA1_Contig0.frz3.gene1', '&gt;MhA1_Contig0.frz3.gene1', '&gt;MhA1_Contig0.frz3.gene1']
  gene.append(g)
  seq = ["ATGGAC", "GGCACAGC", "CCACC"]
  seq.append('ACTGAAA')
  
  print json.dumps(output)
  </code></pre>
  
  <p>===== test.php</p>
  
  <pre><code>$output=null;
  $g = "0.CUFF.6.1_2";
  $data = escapeshellarg(json_encode($g));
  exec("python /mysql/getSeq/test.py $data 2&gt;&amp;1 &amp;", $output );
  //exec("python /mysql/getSeq/test.py $data 2&gt;&amp;1", $output );   --&gt; worked fine
  //exec("python /mysql/getSeq/test.py $data", $output );   --&gt; worked fine
  //print_r($output);   --&gt; worked fine
  //var_dump($output);   --&gt; worked fine
  
  $result = json_decode( exec("python /mysql/getSeq/test.py $data", $output ) , true);
  //print_r( $result);   --&gt; worked fine 
  //var_dump($result);   --&gt; worked fine
  echo $result[0][0] . "&lt;br&gt;";
  echo $result[0][1] . "&lt;br&gt;";
  echo $result[1][0] . "&lt;br&gt;";
  echo $result[1][1] . "&lt;br&gt;";
  </code></pre>
  
  <p>===== results on the web page</p>
  
  <pre><code>&gt;MhA1_Contig0.frz3.gene1
  ATGGAC
  &gt;MhA1_Contig0.frz3.gene1
  GGCACAGC
  </code></pre>
  
  <h1>=========== REAL CASE ===========</h1>
  
  <p>===== real.py</p>
  
  <pre><code>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] = "&gt;" + gene[i]
      output[i][1]= str(matrix[i])[2:-4]
  
  print json.dumps(output)
  # print output
  </code></pre>
  
  <p>===== real.php</p>
  
  <pre><code>$output=null;
  $g = "0.CUFF.6.1_2";
  $data = escapeshellarg(json_encode($g));
  exec("python /mysql/getSeq/real.py $data  2&gt;&amp;1 &amp;", $output );
  //exec("python /mysql/getSeq/real.py $data  2&gt;&amp;1", $output );  --&gt; no difference in results
  //exec("python /mysql/getSeq/real.py $data", $output );   --&gt; no difference in results
  $result = json_decode( exec("python /mysql/getSeq/real.py $data 2&gt;&amp;1", $output ) , true);
  print_r($output) . "&lt;br&gt;";
  //var_dump($output) . "&lt;br&gt;";
  //print_r( $result) . "&lt;br&gt;";
  //var_dump($result) . "&lt;br&gt;";
  //echo $result[0][0] . "&lt;br&gt;";
  //echo $result[0][1] . "&lt;br&gt;";
  //echo $result[1][0] . "&lt;br&gt;";
  //echo $result[1][1] . "&lt;br&gt;";
  </code></pre>
  
  <p>===== results on the web page</p>
  
  <p>print_r($output); --> error</p>
  
  <pre><code>Array ( [0] =&gt; Traceback (most recent call last): [1] =&gt; File "/mysql/getSeq/getOne.py", line 3, in [2] =&gt; inF = file("getSeq_Mh.fasta", "r") [3] =&gt; IOError: [Errno 2] No such file or directory: 'getSeq_Mh.fasta' [4] =&gt; Traceback (most recent call last): [5] =&gt; File "/mysql/getSeq/real.py.py", line 3, in [6] =&gt; inF = file("getSeq_Mh.fasta", "r") [7] =&gt; IOError: [Errno 2] No such file or directory: 'getSeq_Mh.fasta' )
  </code></pre>
  
  <p>var_dump($output); --> error</p>
  
  <pre><code>array(8) { [0]=&gt; string(34) "Traceback (most recent call last):" [1]=&gt; string(95) " File "/mysql/getSeq/getOne.py", line 3, in " [2]=&gt; string(38) " inF = file("getSeq_Mh.fasta", "r")" [3]=&gt; string(63) "IOError: [Errno 2] No such file or directory: 'getSeq_Mh.fasta'" [4]=&gt; string(34) "Traceback (most recent call last):" [5]=&gt; string(95) " File "/mysql/getSeq/real.py.py", line 3, in " [6]=&gt; string(38) " inF = file("getSeq_Mh.fasta", "r")" [7]=&gt; string(63) "IOError: [Errno 2] No such file or directory: 'getSeq_Mh.fasta'" } 
  </code></pre>
  
  <p>print_r( $result); --> error</p>
  
  <pre><code>(nothing)
  </code></pre>
  
  <p>var_dump($result); --> error</p>
  
  <pre><code>NULL 
  </code></pre>
  
  <h1>=========== PYTHON ITSELF ===========</h1>
  
  <pre><code>[['&gt;MhA1_Contig0.frz3.gene1', 'ATGGACT'], ['&gt;MhA1_Contig0.frz3.gene1', 'GGCACAGC'], ['&gt;MhA1_Contig0.frz3.gene1', 'CCACC']]
  </code></pre>