英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论