Python脚本只是打印出空白页面

时间:2015-04-22 18:26:53

标签: python xampp anaconda pillow

我正在使用xampp并且我能够在其上运行简单的python脚本,因此xampp设置正常用于python。我正在尝试使用枕头。

我已经安装了anaconda并在终端

中进行了跟踪
 conda install pillow

如果我在终端下面运行test.py,它运行正常。它打印格式,大小,模式。

但如果我在网络浏览器上试用它,我会得到空白页。

这是test.py

#!/Library/Frameworks/anaconda/bin/python

print("Content-Type: text/html")
print()
print("<html><head><title>Python</title></head><body>")
from PIL import Image, ImageFilter
original = Image.open("Lenna.png")
print("<h5>The size of the Image is: </h5>")
print("<h2>size " + original.format, original.size, original.mode + "</h2>")

如果我使用cgitb.enable(),我会收到以下错误

    A problem occurred in a Python script. Here is the sequence of function calls leading up to the error, in the order they occurred.

 /Applications/XAMPP/xamppfiles/cgi-bin/test7.py in ()
      8 print("<html><head><title>Python</title></head><body>");
      9 print("<h5>The size of the Image is: </h5>");
=>   10 from PIL import Image
     11 original = Image.open("Lenna.png");
     12 width, height = original.size;
PIL undefined, Image undefined
 /Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/PIL/Image.py in ()
     61     # Also note that Image.core is not a publicly documented interface,
     62     # and should be considered private and subject to change.
=>   63     from PIL import _imaging as core
     64     if PILLOW_VERSION != getattr(core, 'PILLOW_VERSION', None):
     65         raise ImportError("The _imaging extension was built for another "
PIL undefined, _imaging undefined, core = <PIL.Image._imaging_not_installed object>
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/PIL/_imaging.so, 2): Library not loaded: @loader_path/.dylibs/libtiff.5.dylib Referenced from: /Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/PIL/_imaging.so Reason: Incompatible library version: _imaging.so requires version 8.0.0 or later, but libtiff.5.dylib provides version 6.0.0 
      args = ('dlopen(/Library/Frameworks/Python.framework/Vers...later, but libtiff.5.dylib provides version 6.0.0',) 
      msg = 'dlopen(/Library/Frameworks/Python.framework/Vers...later, but libtiff.5.dylib provides version 6.0.0' 
      name = '_imaging' 
      path = '/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/PIL/_imaging.so' 
      with_traceback = <built-in method with_traceback of ImportError object>

1 个答案:

答案 0 :(得分:0)

有点反答,但除非你出于学术原因这样做,否则你应该尝试一个更标准的框架:

我上面列出的两个框架是极简主义框架的例子,它们不会妨碍你。他们负责运行您的应用程序并设置路线。使用Flask的最低要求是:

from flask import Flask
from PIL import Image, ImageFilter

app = Flask(__name__)

@app.route("/")
def myview():
    original = Image.open("Lenna.png")

    return """
        <html><head><title>Python</title></head>
        <body>
        <h5>The size of the Image is: </h5>
        <h2>size {format}, {size}, {mode}</h2>
        </body></html>""".format(
            format=original.format,
            size=original.size,
            mode=original.mode)

app.run()

接下来,如果您想让您的应用程序可用,您只需要在XAMPP中设置反向代理(easy-peasy)。我强烈建议使用Flask;它带有一个很棒的页内调试器,由其姐妹项目Werkzeug(点亮“工具箱”)提供。

Werkzeug调试器:

Werkzeug debugger