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

北城余情 提交于 2019-12-11 10:45:22

问题


This is a rather complicated problem.

My Python script uses the module PIL which is loaded by:

from PIL import ImageTk, Image

The script works fine in the IDE and also when I start it from the cmd:

python script.py

But when I try to open it by double-clicking it gives the ImportError: no module named PIL. I'm using python 2.7 in Anaconda. I uninstalled PIL and installed Pillow, but the problem persists. I copied the PIL directory to the working directory, but then I get ImportError: DLL load failed: %1 is not a valid Win32 application. I also need to compile it as an executable, but opening the executable gives yet another error: version conflict for package "Tcl": have 8.5.18, need exactly 8.5.15.

It compiles just fine when I leave out the part of the script that uses PIL. Is there any way to have my cake and eat it too - to use images in the tkinter interface AND compile it with py2exe?


回答1:


Turns out the double-click was indeed using a different interpreter. The double click used the 32-bit python version in Program Files (x86) whereas opening it via command line by typing in python script.py used the 64-bit python version in the Anaconda installation directory. Apparently I had installed python from python.org, then had forgotten all about it and installed Anaconda. Changing the file associations fixed the issue, thank you for your help guys.



来源:https://stackoverflow.com/questions/35787195/importerror-no-module-named-pil-when-opening-the-py-file-directly-in-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!