Python Script Won't Run Via TextMate, OK in IDLE and Eclipse

不羁岁月 提交于 2019-12-30 07:43:46

问题


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

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.

Anyone know why?

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

回答1:


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.

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)

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.




回答2:


Textmate is using the built-in Python, rather than respecting the shebang line. You'd probably have to hack the bundle code to use the right python.



来源:https://stackoverflow.com/questions/4516358/python-script-wont-run-via-textmate-ok-in-idle-and-eclipse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!