I have a python program that works fine. When I run it through terminal I get the wanted output. But now I'm trying to execute it through a PHP script, and I keep receiving a import error message.
After some research, I edited my python code, adding the line
print (sys.path)
The output was different when run by terminal and by PHP script. Then I added the line
sys.path.insert(0,path)
to add the missing path during the PHP execution, but it didn't solve the problem (even though I can see the path in the list when I print the sys.path
).
I also tried variations like os.environ['PATH'] += ':'+path
. But it didn't work either.
This is my PHP code that is not working:
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
exec('/usr/bin/python3 /var/teste.py 2>&1', $out);
print_r($out);
?>
But when I type:
python3 /var/teste.py
on terminal, it works. When I'm executing the PHP script I have the following message:
import mysql.connector [3] => ModuleNotFoundError: No module named 'mysql'.