1

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.")
melpomene
  • 84,125
  • 8
  • 85
  • 148
Farrell
  • 13
  • 1
  • 4
  • There's nothing wrong with asking for homework help. Just as long as you're not asking us to do your homework. – Falmarri Dec 23 '10 at 06:52

2 Answers2

2

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.

Community
  • 1
  • 1
Ned Deily
  • 83,389
  • 16
  • 128
  • 151
  • That sure seems to explain it. I noticed that when I ran it in terminal it seemed to have a problem with the Python 3 syntax. – Farrell Dec 23 '10 at 14:04
  • Your solution works. I edited TM_PYTHON in the project and it ran properly in Terminal. Thanks very much. – Farrell Dec 23 '10 at 14:18
  • Great! BTW, if you ask a question on StackOverflow, the expectation is that you will either mark an answer as accepted (that's the way answerers receive reputation points) or refine your question until you get an acceptable answer. – Ned Deily Dec 23 '10 at 15:12
0

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.

Tyler Eaves
  • 12,879
  • 1
  • 32
  • 39