ImportError: no module named PIL when opening the .py file directly in Windows

ImportError: no module named PIL when opening the .py file directly in Windows

这是一个比较复杂的问题。

我的 Python 脚本使用模块 PIL,该模块由以下加载:

from PIL import ImageTk, Image

该脚本在 IDE 中运行良好,当我从 cmd:

启动它时也是如此
python script.py

但是当我尝试通过双击打开它时,它给出了 ImportError: no module named PIL。我在 Anaconda 中使用 python 2.7。我卸载了 PIL 并安装了 Pillow,但问题仍然存在。我将 PIL 目录复制到工作目录,但随后出现 ImportError: DLL load failed: %1 is not a valid Win32 application。我还需要将其编译为可执行文件,但打开可执行文件会出现另一个错误:包版本冲突 "Tcl": have 8.5.18, need exactly 8.5.15.

当我省略脚本中使用 PIL 的部分时,它编译得很好。有什么办法既能吃蛋糕又能吃 - 在 tkinter 界面中使用图像并用 py2exe 编译它?

原来双击确实使用了不同的解释器。双击使用 Program Files (x86) 中的 32 位 python 版本,而通过命令行输入 python 打开它 script.py 使用 64 位 python 版本在 Anaconda 安装目录中。显然我已经从 python.org 安装了 python,然后完全忘记了它并安装了 Anaconda。更改文件关联解决了问题,感谢您的帮助。