1

I am using PHP and XAMPP. I have enabled port localhost:8080 and I want to execute a python script (Main.py) from my PHP script (index.php). My PHP script is located at:

/Users/User/.bitnami/stackman/machines/xampp/volumes/root/htdocs/index.php

and the Python script is located at:

/Users/User/.bitnami/stackman/machines/xampp/volumes/root/htdocs/Main.py.

My Python script is the following:

print('Hello World')

My PHP script is the following:

<?php

$output = exec('/usr/local/bin/python3 /Users/User/.bitnami/stackman/machines/xampp/volumes/root/htdocs/Main.py ' . ' 2>&1');
var_dump($output);

?>

(/usr/local/bin/python3 is the output of which python3 at the terminal)

However, the output that I am getting at localhost:8080 is:

string(40) "sh: 1: /usr/local/bin/python3: not found"

I have also tested different variations of my line with exec() by writing for example exec('/Users/User/.bitnami/stackman/machines/xampp/volumes/root/htdocs/Main.py ' . ' 2>&1') but nothing returns the right output.

What am I doing wrong and how can I fix it?

I am aware of similar posts on StackOverflow (Running a Python script from PHP, Can't execute Python script from PHP document, Python Script Failing to Execute from PHP exec(), How to properly call Python 3 script from PHP?) but none of this works as such for me.

I am wondering if my problem is related to the fact that I did not make any configuration to run the Python script on XAMPP as it is described at some posts here (Running Python scripts with Xampp).

5
  • Can you start python3 in the terminal by typing /usr/local/bin/python3 ? Commented May 3, 2018 at 20:33
  • Thank you for your comment. I think that I do since I get the following output when I enter it: Python 3.6.3 (v3.6.3:2c5fed86e0, Oct 3 2017, 00:32:08) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin Type "help", "copyright", "credits" or "license" for more information. Commented May 4, 2018 at 8:50
  • did you put #!/usr/bin/env python at the start of your python script? Commented May 4, 2018 at 12:07
  • So your path to python3 is correct. I use exactly the same setup as you (MacOSX, XAMPP, Python3, PHP) and I do get the desired output when running your code. When I change the directory of python3 "$output = exec('/usr/local/bin/python3...)" to "($output = exec('/usr/local/bin/python...), which does not exist, I get the following error message: string(52) "sh: /usr/local/bin/python: No such file or directory" - so string(52) instead of string(40). There must be some problem with XAMPP config I guess. Commented May 4, 2018 at 20:41
  • Can you try to use localhost/ instead of localhost:8080/ - and run your php script as follows: localhost/Main.py Commented May 4, 2018 at 20:46

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.