简体   繁体   English

无法从 PHP (apache2) 执行 python 脚本

[英]Can't execute python script from PHP (apache2)

I want to execute Python script from PHP file using apache2 on my macos.我想在我的 macos 上使用 apache2 从 PHP 文件执行 Python 脚本。 I am able to execute simple python script like:我能够执行简单的 python 脚本,例如:

From PHP:来自 PHP:

$result = shell_exec("python /Library/…./example.py '$name' ‘$email’ ”);
var_dump($result);

To Python至 Python

import sys

x = sys.argv[1]
y = sys.argv[2]

print("name: " + x + "<br>")
print("email: " + y + "<br>")

and the output is: output 是:

在此处输入图像描述

But when I try to import packages like:但是当我尝试导入包时:

import openpyxl
import numpy as np
import matplotlib.pyplot as plt

I get:我得到:

在此处输入图像描述

My question is, does anyone knows:我的问题是,有谁知道:

1 – How can I make those (and any other) packages work? 1 – 我怎样才能使这些(和任何其他)包工作?

2 – shell_exec is currently executing python2. 2 – shell_exec 当前正在执行 python2。 How can I add python3?( if I write python3 instead of python won't work)如何添加 python3?(如果我写 python3 而不是 python 将不起作用)

In case anyone has the same problem in the future, here's what I found out.如果将来有人遇到同样的问题,这就是我发现的。

By executing some different commands via shell_exec, I noticed that it was behaving differently than my terminal.通过 shell_exec 执行一些不同的命令,我注意到它的行为与我的终端不同。

在此处输入图像描述

So I find out that when I execute python3 over shell_exec, it was not pointing to the right address, then all I had to do was to pass the right address:所以我发现当我在 shell_exec 上执行 python3 时,它没有指向正确的地址,那么我所要做的就是传递正确的地址:

$result = shell_exec("/Library/Frameworks/Python.framework/Versions/3.7/bin/python example.py '$name' ‘$email’ ”);
var_dump($result);

And now the script works:)现在脚本可以工作了:)

Instead of use the whole file path, put the file path in an enviroment var in order to access python / python3 program wherever you want不要使用整个文件路径,而是将文件路径放在环境变量中,以便随时随地访问 python / python3 程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM