0

I want to execute Python script from PHP file using apache2 on my macos. I am able to execute simple python script like:

From PHP:

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

To Python

import sys

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

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

and the output is:

enter image description here

But when I try to import packages like :

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

I get:

enter image description here

My question is, does anyone knows:

1 – How can I make those (and any other) packages work?

2 – shell_exec is currently executing python2. How can I add python3?( if I write python3 instead of python won’t work)

2 Answers 2

1

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.

enter image description here

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:

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

And now the script works :)

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.