0

I want to run my Python script using PHP's shell_exec() and xammp on Mac. When I run it on terminal it is working, but when I run it on browser it shows this error 'sh: 1: python3: not found'

PHP

$command = escapeshellcmd("python3 scrape.py");
$command_output = shell_exec($command." 2>&1");

echo $command_output;

Python

#!/usr/bin/env python3
import cgi,cgitb
import urllib.request

from bs4 import BeautifulSoup
from bs4.element import Comment
from datetime import datetime
import re
import json

print ("Content-Type: text/plain;charset=utf-8\n")

print("hello")
halfer
  • 19,824
  • 17
  • 99
  • 186
ghost
  • 36
  • 6
  • 2
    You're trying to invoke `python3` from php without giving it a path (you're not invoking `scrape.py`, you're passing it as a file argument to `python3`). You can either add its directory to your path, or invoke it with an absolute path. – Tom Karzes Apr 12 '19 at 07:24
  • I edited the code to this escapeshellcmd("/usr/local/bin/python3 scrape.py") and it shows this output 'sh: 1: /usr/local/bin/python3: not found' – ghost Apr 12 '19 at 07:38
  • 1
    Try `ls -l /usr/local/bin/python3` from your shell, verify that it exists and is executable. – Tom Karzes Apr 12 '19 at 07:39
  • it returns this: lrwxr-xr-x 1 amplabinc. wheel 69 Apr 8 15:46 /usr/local/bin/python3 -> ../../../Library/Frameworks/Python.framework/Versions/3.7/bin/python3 – ghost Apr 12 '19 at 08:30
  • Well, a symlink exists for it. Try doing `ls -lL /usr/local/bin/python3` to make sure it resolved (i.e. add `L`). – Tom Karzes Apr 12 '19 at 08:53
  • @TomKarzes it shows this: -rwxrwxr-x 2 root admin 9604 Mar 26 04:52 /usr/local/bin/python3 – ghost Apr 12 '19 at 09:04
  • Well it looks like it's there and it's executable, so I'm not sure what's going on. You can try running it directly by typing `/usr/local/bin/python3` from your shell, but I expect it will work. – Tom Karzes Apr 12 '19 at 09:15
  • Yes it is working in terminal. But still not in browser. Thanks for the help – ghost Apr 12 '19 at 09:31
  • it's working on shared hosting. but in xampp it's not – ghost Apr 12 '19 at 10:15

0 Answers0