ashcrok ashcrok -4 years ago 123
PHP Question

Laravel + Apache executing Python script with matplotlib

I am using php 7 with laravel 5.4 and I'm trying to execute some python scripts that use matplotlib to create some plots.


Python script


Command line outputs:


Creating a small php file

exec('', $output);

will output:

d:\workspace>php test.php
[0] => DONE

Doing the same thing from a laravel controller method:

public function show_with_file($id, $file)

// Execute python script to retrieve dot-plot
$command = escapeshellcmd('d:\\workspace\\');

exec($command, $output);


will dump the output:

array:1 [▼
0 => "DONE"

Everything is running ok until now. But, if we modify the python script to include the matplotlib module:

import matplotlib


executing the script will be successful, executing the
file in command line works fine, but dumping the output result from the Laravel controller gives:



Reading a little about this issue on different posts, I changed the back-end of matplot lib to agg:

import matplotlib


but nothing changed: the script works perfectly in the command line via
, the result is correctly given by executing
php test.php
, but in the laravel controller the result is still missing.

I am confused because it isn't a php issue due to the fact that a small php exec script will do just fine, but under laravel and apache it doesn't and it isn't an issue of file permission due to the fact that the script works if it doesn't include matplotlib.

Anyone has any ideas of what am I missing?

Answer Source

Are you sure there are no errors in your Python code?

Maybe the PHP is trying to run your code on a version of Python where matplotlib is not installed?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download