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

A brief discussion on how to run Python scripts in PHP

青灯夜游
Release: 2023-04-09 20:24:02
forward
4488 people have browsed it

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);
Copy after login

Python code (1.py)

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

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);
Copy after login

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);
Copy after login

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"
Copy after login

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);
Copy after login

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!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template