1

我有一个在 Python Shell 中运行良好的脚本,但是当双击时它无法导入 PIL 模块(但它确实在 Shell 中导入了 PIL 模块)。

怎么了?我需要一个不同的shebang吗?我在 Windows 7 上:

#!/usr/bin/env python
"""

"""

try:
    from   PIL import Image
    import os
    import datetime
    import time

except Exception, e:
    raw_input(str(e))

# Running this script in the Shell, the code gets to here
# Running this script in real life (just double click it) it prints "no module PIL"
4

4 回答 4

4

添加行

import sys
print sys.executable

到你的代码。当你在 shell 中运行它时,你会看到它是一个不同的 python 可执行文件,而不是双击 .py 文件。

如果您想控制使用哪个已安装版本的 python 来运行脚本,而不是简单地运行脚本,请在 windows 命令窗口中键入类似以下内容。

c:\python27\python.exe scriptname.py

根据需要替换 python 目录和脚本名称。

于 2011-06-16T02:57:38.860 回答
2

Windows 忽略了 shebang 行(即使它尊重它,它也无法执行/usr/bin/env——它在 Windows 上不存在)。在资源管理器中双击使用与.py扩展关联的程序启动脚本。检查您是否已将其设置为正确的 Python 版本。

于 2011-06-16T02:01:56.857 回答
0

听起来您为不同版本的 Python 安装了 PIL,而不是设置为在 Windows 中打开 .py 文件。如果您使用的是 Python 2.7, PIL 会自行安装C:\Python27\Lib\site-packages\PIL,在这种情况下,您需要确保 Windows 使用 .py 打开 .py 文件C:\Python27\python.exe。您可以右键单击 .py 文件,单击打开方式 -> 选择默认程序... 将其设置为您安装 PIL 的 Python 版本。

于 2011-06-16T02:04:50.787 回答
0

我也遇到了这个问题,但意识到我需要更新app.yaml文件。将此粘贴到libraries

- name: PIL
  version: "1.1.7"
于 2013-02-18T17:02:42.070 回答