PHP和导入pylab [英] PHP and Importing pylab
问题描述
我有一个调用python程序的PHP脚本。这是php脚本:
I have a PHP script that calls in a python program. Here is the php script:
<?php
$last_line = popen('/Library/Frameworks/Python.framework/Versions/Current/bin/python test.py', 'r');
$results = fgets($last_line);
print $results;
?>
这是test.py的内容:
and this is the content of test.py:
test.py:
import numpy as np
from matplotlib.patches import Ellipse
# import matplotlib.pyplot as plt
# from matplotlib.pyplot import figure, show
# import pylab
print "Hello World!"
现在,这个工作正常,我得到Hello World!在浏览器中。但是,如果我取消注释任何导入(即,将matplotlib.pyplot导入为plt,将matplotlib.pyplot导入为plt或导入pylab),我不会从PHP获得结果。如果有人可以帮我解决这个问题,那就太好了,因为我需要Python中的所有绘图函数。
Now, this works fine and I get "Hello World!" in browser. However, if I uncomment any of the imports (i.e., import matplotlib.pyplot as plt, import matplotlib.pyplot as plt or import pylab) I don't get the result from PHP. It will be great if someone could help me with this as I need all the plotting functions from Python.
推荐答案
matplotlib部分是当python脚本通过php执行时没有执行。
然后我只在我的.py文件中包含以下语句..
The matplotlib part was not getting executed when the python script was executed through php. Then i just include the following statements in my .py file..
import os
os.environ[ 'MPLCONFIGDIR' ] = '/tmp/'
import matplotlib
matplotlib.use('agg')
它有效......
这篇关于PHP和导入pylab的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!