问题
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