0

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.

Motiss
  • 72
  • 1
  • 13
  • Where are `getter.py` and `got` in relation to each other, and to the PHP file? How did you install `got`? – Daniel Roseman Nov 17 '18 at 16:58
  • Everything is in the same folder /var/www/html/test belongs to root user. got was installed with a git clone I just tried to create a `lib` folder, containing a `toto.py` and a `__init__..py`, and including lib.toto does work in PHP. I'll have to dig deeper. – Motiss Nov 17 '18 at 17:09
  • Sorry but that is really unclear. Can you please update your question with the directory structure (eg using `tree`). – Daniel Roseman Nov 17 '18 at 17:13
  • OK, just did that. – Motiss Nov 17 '18 at 17:29
  • Change `import got` to `from . import got` and try again. Can you also try to add the log error? – mostafazh Nov 17 '18 at 18:56
  • I got it. `Nginx` was executing as root, which didn't get all the requirements to run `got`, I needed to reinstall the requirements for all users. Thanks for the time you spend helping me. – Motiss Nov 18 '18 at 18:08

1 Answers1

1

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.

Motiss
  • 72
  • 1
  • 13
  • I switched from apache to nginx and i am calling a python script to execute. For me the problem was fixed by simply install the required python-library with the "-H" parameter. Like that: sudo -H pip install – Updater Feb 24 '22 at 19:28