【问题标题】:Python script just printing out blank pagePython脚本只是打印出空白页
【发布时间】:2015-06-30 14:34:15
【问题描述】:

我正在使用 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>

【问题讨论】:

    标签: python xampp anaconda pillow


    【解决方案1】:

    有点反对,但除非您出于学术原因这样做,否则您应该尝试使用更标准的框架之一:

    我上面列出的两个框架是极简框架的示例,它们不会妨碍您。他们负责运行您的应用程序并设置路线。使用 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 调试器:

    【讨论】:

    • 您的意思是您出于学术原因正在研究 Python 脚本,或者您正在尝试按照您现在的方式(不推荐)专门出于学术原因?
    • 我这样做是出于学术原因,我必须使用枕头库来完成我的作业。我试过 cgitb.enable(),我得到这个错误“不兼容的库版本:_imaging.so 需要版本 8.0.0 或更高版本,但 libtiff.5.dylib 提供版本 6.0.0”
    猜你喜欢
    • 1970-01-01
    • 2015-05-18
    • 1970-01-01
    • 2018-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-27
    • 1970-01-01
    相关资源
    最近更新 更多