Python 脚本无法通过 TextMate 运行,在 IDLE 和 Eclipse 中正常 [英] Python Script Won't Run Via TextMate, OK in IDLE and Eclipse

查看:36
本文介绍了Python 脚本无法通过 TextMate 运行,在 IDLE 和 Eclipse 中正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为家庭作业编写了以下内容,它在运行 Python 3 的 IDLE 和 Eclipse 中运行良好.

I wrote the following for a homework assignment and it works fine in IDLE and Eclipse running Python 3.

但是,我尝试使用新的第 1 行(我在此处找到)从 TextMate 运行它,以将其指向 Mac 上的 Python 3.它似乎正在运行 Python 3,但返回一个错误.它说:EOFError:读取一行时的EOF.它指的是下面的第 5 行.

However, I tried to run it from TextMate with the new line 1 -- which I found here -- to point it to Python 3 on the Mac. It seems to be running Python 3 but returns an error. It says: EOFError: EOF when reading a line. It's referring to line 5 below.

有人知道为什么吗?

顺便说一句,这个 TextMate 问题不是家庭作业的一部分,所以我不想寻求家庭作业帮助.我只想弄清楚如何在 Python 3 中使用 TextMate.

BTW, this TextMate issue is not part of the homework assignment, so I'm not trying to get homework help. I just want to figure out how to use TextMate with Python 3.

#! /usr/local/bin/python3
#
# Tests user string against two conditions.
#
user_string = input("Enter a string that is all upper case and ends with a period: ")
if user_string.isupper() and user_string.endswith("."):
    print("Your string met both conditions.")
else:
    if user_string.isupper():
        print("Your string does not end with a period.")
    elif user_string.endswith("."):
        print("Your string is not all upper.")
    else:
        print("Your string failed both conditions.")

推荐答案

您所看到的问题与 Python 版本无关.问题是 TextMate 不会尝试重定向标准输入,因此,当您通过 TextMate 的 Python bundle Run Script 命令运行时,Python 程序会立即看到文件结尾.如此处解释,TextMate 过去对此更感兴趣,但它使用的机制没有在 OS X 10.6 中运行时间更长,因此该功能被禁用.

The problem you are seeing has nothing to do with the Python version. The problem is that TextMate does not try to redirect standard input so, when you are running via the TextMate's Python bundle Run Script command, the Python program sees an immediate end-of-file. As explained here, TextMate used to be fancier about this but the mechanism it used no longer works in OS X 10.6 so the feature was disabled.

一种解决方案是使用 TextMate 的 Python 包的 Shift-Command-R Run Script in Terminal 命令.这会导致 TextMate 打开一个终端窗口并在那里运行脚本,您可以在那里输入输入.不幸的是,虽然 TextMate 确实使用普通的 Command-R Run Script command 尊重 shebang 行,但它似乎没有使用 Run Script in Terminal 命令.您可以通过各种方式验证自己.尝试在 TextMate 中运行此代码片段:

One solution is to use the Shift-Command-R Run Script in Terminal command of TextMate's Python bundle. This causes TextMate to open a terminal window and run the script there and you can enter the input there. Unfortunately, while TextMate does respect the shebang line with the normal Command-R Run Script command, it doesn't seem to do so with the Run Script in Terminal command. You can verify that yourself in various ways. Try running this code snippet in TextMate:

#! /usr/local/bin/python3
import sys
print(sys.executable)

要解决这个问题,您可以在 TextMate 中设置 TM_PYTHON 环境变量.请参阅此处的答案,了解有关如何执行此操作的更多详细信息.

To get around that, you can set the TM_PYTHON environment variable in TextMate. See the answer here for more details on how to do that.

这篇关于Python 脚本无法通过 TextMate 运行,在 IDLE 和 Eclipse 中正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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