0

I've been working on a project for school and have been running into some trouble with PHP running python scripts. When I run this specific python script in terminal it works perfectly; however, when I run it through PHP's shell_exec the script fails to display anything. I believe it may be due to captionbot, but I am not sure what the problem really is. (Also when I change my python script to something simple like print("hello") it works fine)

Here is the PHP Code

echo exec("python /var/www/html/caption.py 'https://www.guidedogs.org/wp-content/uploads/2017/04/small-boxes-website2.jpg'");

      ?>

Here is the python script

#!/usr/bin/env python
import sys
people = sys.argv[1]
from captionbot import CaptionBot
c = CaptionBot()
desc = c.url_caption(people)
print(desc)
Hudson
  • 29
  • 5
  • Put `2>&1` at the end of the command, so you see the error messages. – Barmar Mar 03 '20 at 20:00
  • Thank you, I did not know of that. When I ran it I got "ImportError: No module named captionbot". It looks like the script can't find the module when running through PHP. Any idea on how to fix that? – Hudson Mar 03 '20 at 20:07
  • How are you running the PHP script? Are you running it from the terminal or from a webserver? – Barmar Mar 03 '20 at 20:19
  • You're probably customizing where you look for Python modules, and need to do the same thing for the process that runs the PHP script. See https://stackoverflow.com/questions/3144089/expand-python-search-path-to-other-source – Barmar Mar 03 '20 at 20:19
  • I'm running it via a web server and I have not done any customization on where my Python modules are stored. Edit: It seems when I run the script from the terminal it does work, but it does not work on the web server. – Hudson Mar 03 '20 at 21:27
  • Maybe you have multiple versions of Python on your system, and you're using a different version. Run `python --version` on the terminal and from PHP. – Barmar Mar 03 '20 at 21:37
  • I just checked and they are both running 2.7.12. – Hudson Mar 03 '20 at 21:41
  • Not related to the problem, but you should consider upgrading. Python 2.x was EOL last month. – Barmar Mar 03 '20 at 21:42
  • What does `env | grep PYTHON` show in your shell? – Barmar Mar 03 '20 at 21:44
  • I just get nothing when I enter that in my shell. – Hudson Mar 03 '20 at 21:47
  • Where is the `captionbot` module located? – Barmar Mar 03 '20 at 21:54
  • It's located in this folder `/home/ubuntu/.local/lib/python2.7/site-packages/` – Hudson Mar 03 '20 at 21:56
  • I'm running out of ideas for why it's not finding it from PHP. What does `print(sys.path)` print from PHP? – Barmar Mar 03 '20 at 21:59
  • For references, see https://leemendelowitz.github.io/blog/how-does-python-find-packages.html – Barmar Mar 03 '20 at 22:00
  • Ok, I finally got it to work. Apparently the folder captionbot was located in was not in the system path for python. I manually copied it over and now it works. I have no idea why python installed the package there but who knows. Thank you so much for your help! – Hudson Mar 03 '20 at 22:06

0 Answers0