Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python module exists in terminal but not when run through PHP's shell_exec

When i run a python script in terminal it works just fine. I then proceed to run the exact same script via PHP's shell_exec from localhost.

shell_exec("python /Applications/MAMP/htdocs/pharm/Webmaps.py")

The error it gives is:

Traceback (most recent call last): File "/Applications/MAMP/htdocs/pharm/Webmaps.py", line 1, in import folium ImportError: No module named folium

It doesn't recognize the modules that are installed. Needless to say that it works just fine from windows on localhost.

like image 218
Dimitrios Arampatzis Avatar asked Oct 29 '22 21:10

Dimitrios Arampatzis


1 Answers

If you have multiple versions of Python on your machine, this may not work. Essentially what I think is happening is that the version of Python that is being called when you use shell_exec is different from the one that you know folium is installed in. Try creating a file like below:

# test.py
import sys
print(sys.version_info)

and use shell_exec("python /path/to/test.py"). Then try python /path/to/test.py. I expect that it will be different. Even if it is not different, it still could mean that you have two or more Python installations that are the exact same versions (unlikely, but possible). You can run the following to check that. Try both shell_exec("which python") and which python. I'd venture a guess that that should give you enough information to solve this problem.

like image 192
kdheepak Avatar answered Nov 15 '22 06:11

kdheepak