使用shell_exec()在PHP代码中运行Python脚本时出现问题。

huangapple go评论102阅读模式
英文:

problem when using shell_exec() in php code to run python script

问题

我有一个可以运行Python代码的PHP代码,但它不起作用。
操作系统:Fedora 38
服务器:本地主机,使用Apache

```php
$command = 'py testCODES.py';
$output = shell_exec($command.' 2>&1');

它给我返回了这个错误:

找不到可执行的Python程序

有什么解决办法吗?

我尝试了以下这些命令:

sudo chown -R apache:apache /var/www/
sudo chmod -R 755 /var/www
sudo chmod +x *.py

<details>
<summary>英文:</summary>

I have a php code that run pyhton code but is not working
os: fedora 38
server: localhost using apache


$command = 'py testCODES.py';
$output = shell_exec($command.' 2>&1');

it gives me this error

No executable found for Python


any solutions? 

i tried solutions these commands:


sudo chown -R apache:apache /var/www/
sudo chmod -R 755 /var/www
sudo chmod +x *.py

 

</details>


# 答案1
**得分**: 0

似乎当PHP运行`shell_exec(…)`时,它无法访问包含可执行文件的目录列表的`$PATH`环境变量。

避免此问题的一种方法是提供完整的文件路径,这样shell就不必猜测文件的位置:

```shell
/usr/bin/python /var/www/[…]/testCODES.py

您可以运行whereis python来查找二进制文件的位置。

英文:

It looks like when PHP runs shell_exec(…), it doesn't have access to the $PATH env var that contain a list of directories that contain the executables.

One way to avoid this issue is to provide full paths so that the shell doesn't have to guess the locations of files:

/usr/bin/python /var/www/[…]/testCODES.py

You can run whereis python to find where is the binary.

huangapple
  • 本文由 发表于 2023年7月13日 18:46:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76678493.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定