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

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

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

问题

  1. 我有一个可以运行Python代码的PHP代码,但它不起作用。
  2. 操作系统:Fedora 38
  3. 服务器:本地主机,使用Apache
  4. ```php
  5. $command = 'py testCODES.py';
  6. $output = shell_exec($command.' 2>&1');

它给我返回了这个错误:

  1. 找不到可执行的Python程序

有什么解决办法吗?

我尝试了以下这些命令:

  1. sudo chown -R apache:apache /var/www/
  2. sudo chmod -R 755 /var/www
  3. sudo chmod +x *.py
  1. <details>
  2. <summary>英文:</summary>
  3. I have a php code that run pyhton code but is not working
  4. os: fedora 38
  5. server: localhost using apache

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

  1. it gives me this error

No executable found for Python

  1. any solutions?
  2. i tried solutions these commands:

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

  1. </details>
  2. # 答案1
  3. **得分**: 0
  4. 似乎当PHP运行`shell_exec(…)`时,它无法访问包含可执行文件的目录列表的`$PATH`环境变量。
  5. 避免此问题的一种方法是提供完整的文件路径,这样shell就不必猜测文件的位置:
  6. ```shell
  7. /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:

  1. /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:

确定