1

I'm using php's exec() command to run a python script but I'm receiving the following error:

Array ( [0] => Traceback (most recent call last): [1] => File "/vagrant/SRC_Local_Website/EAF_MODEL/EAF_Model.py", line 20, in [2] => from pylab import * [3] => File "/usr/lib/pymodules/python2.7/pylab.py", line 1, in [4] => from matplotlib.pylab import * [5] => File "/usr/lib/pymodules/python2.7/matplotlib/__init__.py", line 774, in [6] => rcParams = rc_params() [7] => 
File "/usr/lib/pymodules/python2.7/matplotlib/__init__.py", line 692, in rc_params [8] => fname = matplotlib_fname() [9] => File "/usr/lib/pymodules/python2.7/matplotlib/__init__.py", line 604, in matplotlib_fname [10] => 
fname = os.path.join(get_configdir(), 'matplotlibrc') [11] => File "/usr/lib/pymodules/python2.7/matplotlib/__init__.py", line 253, in wrapper [12] => ret = func(*args, **kwargs) [13] => 
File "/usr/lib/pymodules/python2.7/matplotlib/__init__.py", line 478, in _get_configdir [14] => raise RuntimeError("Failed to create %s/.matplotlib; consider setting MPLCONFIGDIR to a writable directory for matplotlib configuration data"%h) [15] => RuntimeError: Failed to create /var/www/.matplotlib; consider setting MPLCONFIGDIR to a writable directory for matplotlib configuration data )

From what I understand, python is unable to find the matplotlib and pylab modules.

Some thing's I'd like to note:

  • I've run: sudo apt-get install python-numpy python-scipy python-matplotlib successfully.
  • If I run this python file directly from the shell (e.g. $python myFile.py) the program runs fine with no errors.
  • In my php file I've tried to specify where these files can be found with putenv('PYTHONPATH= ... (see below)

For example, my imports are as follows:

from casadi import *
from numpy import *
from pylab import *
import matplotlib.pyplot as plt

Originally, cadADi couldn't be found so I added:

putenv('PYTHONPATH=$PYTHONPATH:/vagrant/SRC_Local_Website/EAF_MODEL/python_plugins/casadi-py27-np1.9.1-v3.0.0');

and that was no longer an issue.

Similarly with numpy I had to add:

putenv('PYTHONPATH=$PYTHONPATH:/usr/lib/python2.7/dist-packages');

Lastly, I tried to add:

putenv('PYTHONPATH=$PYTHONPATH:/usr/lib/pymodules/python2.7');

Which is where mathplotlib and pylab are located (I don't know why they're separated from dist-packages, that's just where they were automatically installed to), but unfortunately I'm still receiving the error above.

Question: What could be the cause of this issue?

Paul Warnick
  • 903
  • 2
  • 13
  • 26
  • Could you check the working directory of your script (the one using exec) vs the location of the Python interpreter? (`import os; print(os.getcwd()`)? Also, have you tried running your code from a portable Python distribution (for example WinPython)? – armatita Aug 27 '16 at 13:45
  • @armatita Thank you, I've just checked both locations, the php script and python file are both located in /vagrant/SRC_Local_Website/EAF_MODEL and when I run the command you specified it also returns the above path. I haven't yet used a portable distribution but I will look into it right now. – Paul Warnick Aug 29 '16 at 14:33
  • I meant the location of your Python interpreter, not the Python script. The Python script was being run (otherwise there would be nothing to "compile" your script and no error would exist). My suspicion is that you are running the Python script but the system path is not what you think it is (and so the imports fail). – armatita Aug 29 '16 at 14:45
  • I may be slightly misunderstanding you here but following the post found here (http://stackoverflow.com/questions/2589711/find-full-path-of-python-interpreter) the output is `/usr/bin/python`. Also the command I'm using to run the python script in php is: `exec('/usr/bin/python /vagrant/SRC_Local_Website/EAF_MODEL/EAF_Model.py 2>&1', $output);` – Paul Warnick Aug 29 '16 at 14:57
  • 1
    That is strange? That command should give you a full path (so it should finish with the python executable). But notice I'm a windows user and that path seems to me like the folder for the native python interpreter in a Linux system (perhaps Ubuntu?). Can you confirm that you only have a python version in your pc? (try: `whereis python` in the system shell or perhaps `whereis python2.7`). If you do find you have more than one version try to add the actual python distribution you want to use in your exec command. Sorry for not having a more ready answer. At this point I'm as mystified as you. – armatita Aug 29 '16 at 16:07

0 Answers0