Python脚本:在Shell中运行但在现实生活中失败,因为无法导入模块 [英] Python Script: Runs in Shell but fails in real life because cant import module

查看:469
本文介绍了Python脚本:在Shell中运行但在现实生活中失败,因为无法导入模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在Python Shell中运行良好的脚本,但是当双击它时无法导入PIL模块(但它确实导入了Shell中的PIL模块)。

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

有什么问题?我需要一个不同的shebang吗?我在Windows 7上:

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"


推荐答案

添加行

import sys
print sys.executable

到你的代码。你会在shell中运行它时看到它是一个不同的python可执行文件,而不是双击.py文件。

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.

如果你想控制哪个安装版本python用于运行脚本,而不是简单地运行脚本,在Windows命令窗口中键入如下内容。

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

替换python目录和脚本名称适当的。

replacing the python directory and script name as appropriate.

这篇关于Python脚本:在Shell中运行但在现实生活中失败,因为无法导入模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆