0

Trying to call a python (2.6) script from php when a make a GET HTTP request from Android, but if the import numpy is in my script, I get a failure, if I remove it, the script/call works. I am computing the FFT spectrum of a numeric field in MySQL, so I need numpy (or do I?).

I've look at similar questions on SO, and this one, answered by @dietrich-epp, is the best one but when I try the same suggestion, my script still fails.

Here is my imports part of my python script:

import  os, os.path, sys
import numpy
import string
import math
import MySQLdb

And the start of my PHP file:

<?php
putenv('PYTHONPATH=/usr/lib/python2.6/site-packages:');
...

And this how I call my python script:

$command = "/var/www/html/tremcam/tst0.py localhost baseline@nd.edu 2>&1";
$pid = popen( $command,"r");
while( !feof( $pid ) )
{
 #echo fread($pid, 256);
 print(json_encode(fread($pid, 256)));
 flush();
 ob_flush();
 usleep(100000);
}
pclose($pid);

And my environment variables have the PYTHONPATH set to:

/var/www/html/tremcam/pythons
/usr/lib/python2.6/site-packages
/usr/lib
/usr/lib/python26.zip
/usr/lib/python2.6
/usr/lib/python2.6/plat-linux2
/usr/lib/python2.6/lib-tk
/usr/lib/python2.6/lib-old
/usr/lib/python2.6/lib-dynload
/usr/lib/python2.6/site-packages/gtk-2.0
/usr/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg-info
/usr/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg-info

Should this (PYTHONPATH=/usr/lib/python2.6/site-packages) be pointing at /usr/lib/python2.6/site-packages/numpy instead?

I will try using passthru() instead of the way I currently have it in PHP. Please note, that after I am able to import numpy as np, the code will take several seconds to complete, will that also be an issue?

Update: if I use: passthru('/var/www/html/tremcam/tst0.py localhost baseline@nd.edu 2>&1',$result);

I get exit status code 139, so that isn't very helpful to me, what do you think?

Community
  • 1
  • 1
sAguinaga
  • 638
  • 13
  • 31

1 Answers1

2

Try with setsebool httpd_tmp_exec on if it works make it permanent with setsebool -P httpd_tmp_exec on

It should be SELinux denying Apache your pytonpath directories (and yes you should provide the very numpy folder if it still doesn't work, but it shouldn't be necessary at all).

A slightly less clean approach is to comment this line import ctypeslib in /<your numpy path>/numpy/__init__.py

I had the same problem and both the approaches worked fine (the latter though may lead to some inconsistency in later developments therefore I suggest you to try setsebool first)

Earendel
  • 21
  • 2