phppythonpython-importcross-language

Importing library in Python called by PHP


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 :

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.


Solution

  • 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.