Importing library in Python called by PHP

1k Views Asked by At

I'm trying to call a Python script from a PHP file, but it fails when I have to load a local library. My PHP is able to call my python if it doesn't load local libraries, and my python script works when launched manually.

Here are minimal (non) working files :

index.php

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8"/>
  </head>
  <body>
    <?php
      $command = escapeshellcmd ("./getter.py") ;
      $output = shell_exec ($command) ;
      echo "<p>$output</p>" ;
    ?>    
  </body>
</html>

getter.py

#!/usr/bin/env python2
import got
if __name__ == "__main__" :
  print "Python2 working"

Where got is the famous local library.

When I manually launch my ./getter.py the print is happening, but my webpage doesn't display anything. When I comment the import got the webpage is also displaying the print.

Additional information :

  • OS : Ubuntu 18.04.1
  • Server web : nginix 1.14.0
  • PHP : 7.2
  • Python : 2.7.15rc1
  • I am a newbee in web language so I am not aware of the debbuging tools yet
  • There is no problems when importing a non-local library (such as os, csv, or others)
  • I tried to replace the $command = escapeshellcmd ("./getter.py") ; $output = shell_exec ($command) ; by a simple $output = shell_exec ("./getter.py") ;
  • I tried to replace #!/usr/bin/env python2 by #!/usr/bin/python2
  • When I ask PHP or Python to give me their current working directory, they both return than they are in /var/html/www/test, as planned.

Here is a folder tree :

├── getter.py
├── got
│   ├── __init__.py
│   ├── manager
│   │   ├── __init__.py
│   │   ├── TweetCriteria.py
│   │   └── TweetManager.py
│   └── models
│       ├── __init__.py
│       └── Tweet.py
├── index.php
├── __init__.py
└── lib
    ├── __init__.py
    └── toto.py

Thank you much.

1

There are 1 best solutions below

1
On BEST ANSWER

This was actually caused by a dependency problem. Some required libraries where installed only for some users. And Nginx couldn't access it as root, while the normal user could.