Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache can't access Python's module 'numpy' - linux

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.

like image 576
konkri Avatar asked Jul 25 '26 08:07

konkri


1 Answers

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.

like image 189
randomir Avatar answered Jul 27 '26 09:07

randomir



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!