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.
Use-case:
Python script script.py:
print("DONE")
Command line outputs:
d:\workspace>python sample.py
DONE
Creating a small php file test.php:
<?php
exec('sample.py', $output);
print_r($output);
?>
will output:
d:\workspace>php test.php
Array
(
[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\\sample.py');
exec($command, $output);
dd($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
print("DONE")
executing the script will be successful, executing the test.php file in command line works fine, but dumping the output result from the Laravel controller gives:
[]
Note:
Reading a little about this issue on different posts, I changed the back-end of matplot lib to agg:
import matplotlib
matplotlib.use('agg')
print("DONE")
but nothing changed: the script works perfectly in the command line via python sample.py, 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?