I run apache2 on Ubuntu 17.10. Inside a php file there is:
<?php
exec("python3 test_python.py 2>&1",$res);
for ($i=0; $i < sizeof($res); $i++) {
echo $res[$i] . '<br>';
}
?>
and inside test_python.py there is:
import numpy as np
print("it works from here.");
Unfortunately, the result i get is:
Traceback (most recent call last): File "test_python.py", line 1, in import numpy as np ModuleNotFoundError: No module named 'numpy'
When i run the script my self through terminal like:
python3 test_python.py
it works fine. I suspect there's a problem with permissions but I haven't found a solution yet.
This looks like a PATH/PYTHONPATH issue. The python3 you are running from your interactive shell might not be the same as the one Apache/PHP runs; and/or the sys.path might differ in both cases, so numpy is found in one case, but not the other.
The safest and the most robust way for running your Python script with dependencies, is to create a virtual environment with virtualenv, activate it, install your deps there:
cd /path/to/project # go to your project dir
virtualenv -p python3 env # create python3-based virtual environment
. env/bin/activate # activate the virtual environment in env/
pip install numpy # install all dependencies
deactivate # deactivate env
and then simply run your Python script from PHP by invoking python from the virtual environment just created (env/bin/python):
<?php
exec("/path/to/project/env/bin/python test_python.py 2>&1", $res);
...
All packages installed inside the virtual environment will now be available in test_python.py.
To add other packages, just activate your virtual env again, and pip install there.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With