Robert
Robert

Reputation: 10380

Error running Python 3 from Sublime Text, works in IDLE

I have:

def confirm_title(title):
    print("Is '", title, "' correct?", sep='')
    confirmation = input("Y = yes N = no: ")
    return confirmation

in sublime text 2 and ran it using the following command:

Tools -> Build System -> (choose) Python
CMD + B (OSX)

The sublime text console gave me the following error:

File "/Users/robert/Desktop/Python/ex_1.py", line 69
    print("Is '", title, "' correct?", sep='')
                                          ^
SyntaxError: invalid syntax
[Finished in 3.6s with exit code 1]
[shell_cmd: python -u "/Users/robert/Desktop/Python/ex_1.py"]
[dir: /Users/robert/Desktop/Python]
[path: /usr/bin:/bin:/usr/sbin:/sbin]

Now if I ran this on the python IDLE 3.4 it works just fine. Why is it that in sublime text it doesn't? Is it because it's using some other version of python that was preinstalled or what?

Upvotes: 1

Views: 864

Answers (1)

user2489252
user2489252

Reputation:

This is SyntaxError is triggrered because you are using Python 2.x in Sublime.

You can confirm this by e.g.,

by simple execute a

print "hello world"

If this works, you can be sure that you are not using Python 3, but Python 2. Or simply import

from platform import python_version
print python_version

Python 2 supports the call of print as a function in general, but the result gets ugly when you are trying to print a tuple.

print 'Hello, World!'
print('Hello, World!')

Python 2.7.6
Hello, World!
Hello, World!


print 'Python', python_version()
print('a', 'b')
print 'a', 'b'

Python 2.7.6
('a', 'b')
a b

And you can't use the end parameter in Python 2. If you want to print on the same line, you can use this:

print "text", ; print 'print more text on the same line'

text print more text on the same line

Upvotes: 1

Related Questions