Python Script: Runs in Shell but fails in real lif

2019-09-04 13:49发布

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).

What 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"

4条回答
smile是对你的礼貌
2楼-- · 2019-09-04 14:28

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"
查看更多
小情绪 Triste *
3楼-- · 2019-09-04 14:31

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.

查看更多
不美不萌又怎样
4楼-- · 2019-09-04 14:34

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.

查看更多
Emotional °昔
5楼-- · 2019-09-04 14:38

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.

查看更多
登录 后发表回答