gpt4 book ai didi

Python 脚本无法通过 TextMate 运行,但在 IDLE 和 Eclipse 中正常

转载 作者:行者123 更新时间:2023-12-01 06:13:34 28 4
gpt4 key购买 nike

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

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

有人知道为什么吗?

顺便说一句,这个 TextMate 问题不是家庭作业的一部分,所以我不想寻求家庭作业帮助。我只是想弄清楚如何将 TextMate 与 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 包 Run Script 命令运行时,Python 程序会立即看到文件结尾。 As explained here ,TextMate 曾经对此比较感兴趣,但它使用的机制在 OS X 10.6 中不再起作用,因此该功能被禁用。

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

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

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

关于Python 脚本无法通过 TextMate 运行,但在 IDLE 和 Eclipse 中正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4516358/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com