Python Script: Runs in Shell but fails in real life because cant import module
I have a script that runs fine in the Python Shell, but when double clicked it fails to import the PIL module(but it does import the PIL module in the Shell).
W开发者_JAVA百科hat is wrong? Do I need a different shebang? I am on 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"
Add the lines
import sys
print sys.executable
to your code. You'll see that it's a different python executable when you run it in the shell, vs. double clicking the .py file.
If you want to control which installed version of python is used to run your script, rather than simply running the script, type something like the following in a windows command window.
c:\python27\python.exe scriptname.py
replacing the python directory and script name as appropriate.
Windows ignores the shebang line (even if it would respect it, it wouldn't be able to execute /usr/bin/env
-- it doesn't exist on Windows). Double-clicking in the explorer starts the script with the program associated with the .py
extension. Check that you have set this to the right version of Python.
Sounds like you have PIL installed for a different version of Python than is set to open .py files in Windows. PIL installs itself into C:\Python27\Lib\site-packages\PIL
if you are using Python 2.7, in which case you'd want to make sure Windows opens .py files with C:\Python27\python.exe
. You can right click on the .py file, click Open With -> Choose default program... to set it to the version of Python you've installed PIL for.
I was having this problem too but realized I needed to update the app.yaml
file. Paste this under libraries
:
- name: PIL
version: "1.1.7"
精彩评论