Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python import fails when called from PHP

I'm having a puzzling problem when trying to import a module in python only when the script is called from php via system or exec.

From the python shell:

import igraph #This works.

if the previous line was in a file, say, test_module.py, then:
python test_module.py in the bash works.

Within PHP:
exec("python test_module.py",$output,$retval) -> fails : $retval = 1.

However, if the script is instead : import math, then this is fine.

Anybody ever dealt with something similar?

like image 457
Alex Nguyen Avatar asked Nov 06 '22 18:11

Alex Nguyen


2 Answers

This is happening because you have installed those packages under a different user, maybe root, or something else.

How i debugged this, is i checked the output of sys.path for both cases (shell, and php's exec, which has the user www-data by default), and than i compared both.

I noticed the '/root/.local/lib/python2.7/site-packages' path missing when i ran it from PHP, which contained exactly these missing packages. So i just copied the content of this folder to '/usr/lib/python2.7/dist-packages/', which solved the issue.

like image 103
Inc33 Avatar answered Nov 12 '22 16:11

Inc33


one thing to check is sys.path

see what the difference is when called each way

like image 36
cobbal Avatar answered Nov 12 '22 16:11

cobbal