Home  >  Article  >  Backend Development  >  A brief discussion on how to run Python scripts in PHP

A brief discussion on how to run Python scripts in PHP

青灯夜游
青灯夜游forward
2021-03-22 18:42:184434browse

This article will introduce to you how to run Python scripts in PHP. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

A brief discussion on how to run Python scripts in PHP

Note: The environment in which this problem occurred is that it is recommended to use IIS PHP to call the Python script in the windows environment and the premise fails. If your environment does not match the environment of this question. Please avoid automatically.

When product demand analysis reaches a certain stage, certain coordination is required between various departments. Then when we do full-stack development, we need to coordinate these in a general way. There is also a need to activate different modules and different development languages ​​in the background at the same time.

First of all, the background uses the IIS PHP environment and runs on windows. Call Python programs through PHP.

Let’s talk about the problem first: under the premise that both Python and PHP are added to the system’s environment variables (and can run normally in cmd mode), run a.php to call up 1.py

PHPCode (a.php)

$result = exec("py 1.py");
var_dump($result);

Python code (1.py)

print("123");
fl=open('test3.txt', 'w')
fl.write("5,5,5,1,2,3")
fl.close()

The two directory files are in the same directory . And IIS has sufficient executable permissions (in order to reproduce the problem, I wrote the simplest code) and then a running error occurred.

Then I improved the PHP code and checked the error status code of the exec function execution

$result = exec("py 1.py", $array, $ret);
var_dump($result);
var_dump($array);
var_dump($ret);

I found that the error code was 103 (execution error)

[Recommended study: "PHP video tutorial》]

This time I changed the bash in the exec function to the standard output (in order to determine the specific error message)

PS: In bash 0, The three numbers 1 and 2 represent STDIN_FILENO, STDOUT_FILENO, and STDERR_FILENO, which are standard input (usually the keyboard), standard output (usually the display screen, or the user terminal console to be precise), and standard error (error information output).

$result = exec("py 1.py 2>error.txt", $array, $ret);
var_dump($result);
var_dump($array);
var_dump($ret);

After running this time, when I opened error.txt, an error appeared: Can't find a default Python. This kind of error (provided that I added python to the environment variable and can directly run the py file in cmd state) ), I feel like I have encountered an error that I cannot express. After fully checking for errors such as environment variables and executable permissions. I subconsciously changed the execution statement in exec to

"python 1.py 2>error.txt"

The error I got this time was ['python' is not an internal or external command, nor is it an operable program or batch file. 】I even suspected that this computer had an idea of ​​its own. Just after going through all the information. It suddenly occurred to me that it might be because PHP is running on the IIS server, but IIS does not have all user permissions (this is both the advantage and disadvantage of IIS website building directory permission control). Combined with the errors reported in the previous error.txt file. I will make some modifications to the code.

$res = exec("你python的安装绝对路径/python.exe 1.py 2>error.txt", $array, $ret);
var_dump($result);
var_dump($array);
var_dump($ret);

Then add an IIS user control executable permission to the user owner of all files under the python file.

This time PHP was able to find the Python executable exe and successfully executed our code.

PS: IIS Windows is indeed a black hole.

For more programming related knowledge, please visit: Programming Video! !

The above is the detailed content of A brief discussion on how to run Python scripts in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete