1

When I am running my code from the terminal I am getting the output. But when I am running the same from my php page using the following:

$command = escapeshellcmd($commandstring);
$output = shell_exec($command);

It gives no output..no error..nothing..

When I remove this import from my python code

from sklearn.ensemble import RandomForestClassifier

then it shows an error in output

name 'RandomForestClassifier' is not defined

then I used

import sys; print sys.path

to see the differences b/w running from PHP and from terminal

.local/lib/python2.7/site-packages

this one is not there while running from the php.

Any permanent solution for this?

It will be better for me if I can fix it with the php code itself.

Well I am able to run other python code from php easily and they are showing the output. But this one has to import this line

from sklearn.ensemble import RandomForestClassifier
1
  • 1
    Could you please add an example of the output ? and try to use the full path of your $command in shell_exec ! Commented Aug 3, 2016 at 1:52

1 Answer 1

1

You can try to add " 2>&1" at the end of $commandstring, so the stderr will be redirect to stdout and will be catched by shell_exec(). Maybe some error message will appear.

If this didn't solve your issue, try to run a simple "Hello world" python script from your PHP. You will know if the problem come from the PHP part or the python part.

Sign up to request clarification or add additional context in comments.

3 Comments

problem is from python part while importing the "from sklearn.ensemble import RandomForestClassifier"
Can you launch your python script as www-data user ? If not, you can edit /etc/apache2/envvars as root to edit the "export PATH=" part, adding your missing folder here. Restart apache after. (if you're under apache)
"well currently /var/www has chmod 777 permissions" This shouldn't be related to our problem. About the test with www-data user, type "su www-data" then launch your python script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.