4

I am trying to run a python file using PHP, but the file doesn't print Hi and I only print "Whatsup". The script is run when the user clicks a submit button. I am on OSX and all the file paths are correct. I did chmod +x hi.py as suggested in another post

hi.py

#set up classes
# figure out regex
#!/Library/Frameworks/Python.framework/Versions/3.8/bin/python3


print("Hi")

signup.inc.php

else {
      $command=escapeshellcmd('/Users/name/Documents/CPSC_Courses/CPSC353/CoronaVirus/webScrap.py');
      $output = shell_exec($command);
      echo $output;
      echo "Whatsup";
      exit();
    }
Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
that_guy_808
  • 45
  • 1
  • 7
  • Does this answer your question? [Running a Python script from PHP](https://stackoverflow.com/questions/19735250/running-a-python-script-from-php) – EnriqueBet May 05 '20 at 05:59
  • 2
    Unfortunately no. I did chmod +x myscript.py to the file and still nothing. – that_guy_808 May 05 '20 at 06:04
  • Just as a quick test, copy the python script to the same directory as your php script and see if that runs. – Nigel Ren May 05 '20 at 06:07
  • 1
    Just did. Same result. Then I switched the path in signup.inc.php to match the new file and nothing. – that_guy_808 May 05 '20 at 06:10
  • This command: `/usr/bin/python /Users/name/Documents/CPSC_Courses/CPSC353/CoronaVirus/webScrap.py` works from Terminal? – Cyborg May 05 '20 at 06:31

1 Answers1

1

You need to tell which python to use + where it is like:

else {
      $command=escapeshellcmd('/usr/bin/python /Users/name/Documents/CPSC_Courses/CPSC353/CoronaVirus/webScrap.py');
      $output = shell_exec($command);
      echo $output;
      echo "Whatsup";
      exit();
    }

OR for Python3:

else {
      $command=escapeshellcmd('/usr/local/bin/python3 /Users/name/Documents/CPSC_Courses/CPSC353/CoronaVirus/webScrap.py');
      $output = shell_exec($command);
      echo $output;
      echo "Whatsup";
      exit();
    }

Check where your Python is and add FULL path for it.

I would just use:

echo shell_exec("/usr/local/bin/python3 /Users/name/Documents/CPSC_Courses/CPSC353/CoronaVirus/webScrap.py");
Cyborg
  • 1,437
  • 19
  • 40
  • 1
    $command = escapeshellcmd('/Library/Frameworks/Python.framework/Versions/3.8/bin/python3 /Users/ name/Documents/CPSC_Courses/CPSC353/CoronaVirus/includes/webScrap.py'); Changed the command to show this but still get the same result. Used "which python3" to get path to python3 – that_guy_808 May 05 '20 at 06:13
  • If thats where your Python is then it will work. Why you use escapeshellcmd its not dependent by user input? – Cyborg May 05 '20 at 06:17
  • 1
    That's just what the other stack overflow question answers suggested. What should I be using? – that_guy_808 May 05 '20 at 06:19
  • 1
    Is there another way to find my python? If I got through my files I have a folder called python 3.8 but there is no .exe file to path to. – that_guy_808 May 05 '20 at 06:20
  • did "which python" got /usr/bin/python. Tried it and same result – that_guy_808 May 05 '20 at 06:23
  • You see `Whatsup` only? Or is issue with your ELSE statement? I use it this way and it works fine. – Cyborg May 05 '20 at 06:29
  • Ya I only see "Whatsup". I have the command and echo $output before the echo "Whatsup". So in order to print "Whatsup" I have to have entered the Else statement right? – that_guy_808 May 05 '20 at 06:32
  • Switched to echo shell_exec( ); however still no luck. I only see "Whatsup"... I just want it to say "Hi" :( – that_guy_808 May 05 '20 at 06:36
  • Yes, correct. From php file try: `echo shell_exec("/usr/bin/python -V");` – Cyborg May 05 '20 at 06:36
  • Just tried it. Still only see "Whatsup" I also tried with python3 filepath and same result – that_guy_808 May 05 '20 at 06:38
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/213136/discussion-between-cyborg-and-that-guy-808). – Cyborg May 05 '20 at 06:39