Python SyntaxError: invalid syntax end=''

Question:

I am studying the book “Head First Python” and I’m having trouble this code:

data = open('sketch.txt')

for each_line in data:
    (role, line_spoken) = each_line.split(':')
    print(role, end='')
    print(' said: ', end='')
    print(line_spoken, end='')

data.close()

Error:

  File "Aula 3.py", line 12
      print(role, end='')
               ^
SyntaxError: invalid syntax

sketch.txt:

Man: Is this the right room for an argument?
Other Man: I've told you once.
Man: No you haven't!
Other Man: Yes I have.
Man: When?
Other Man: Just now.
Man: No you didn't!
Other Man: Yes I did!
Man: You didn't!
Other Man: I'm telling you, I did!
Man: You did not!
Other Man: Oh I'm sorry, is this a five minute argument, or the full half hour?
Man: Ah! (taking out his wallet and paying) Just the five minutes.
Other Man: Just the five minutes. Thank you.
Other Man: Anyway, I did.
Man: You most certainly did not!
Other Man: Now let's get one thing quite clear: I most definitely told you!
Man: Oh no you didn't!
Other Man: Oh yes I did!
Man: Oh no you didn't!
Other Man: Oh yes I did!
Man: Oh look, this isn't an argument!
(pause)
Other Man: Yes it is!
Man: No it isn't!

I’m two days trying to figure out why the code is not working. The error is always shown in “end =””.

Asked By: Jonny

||

Answers:

It seems like you’re using Python 2.x, not Python 3.x.

Check your python version:

>>> import sys
>>> sys.version
'2.7.5 (default, May 15 2013, 22:44:16) [MSC v.1500 64 bit (AMD64)]'
>>> print(1, end='')
  File "<stdin>", line 1
    print(1, end='')
                ^
SyntaxError: invalid syntax

In Python 3.x, it should not raise Syntax Error:

>>> import sys
>>> sys.version
'3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:06:53) [MSC v.1600 64 bit (AMD64)]'
>>> print(1, end='')
1>>>
Answered By: falsetru
>>> import sys
>>> print(sys.version)
2.7.8 (default, Jun 30 2014, 16:08:48) [MSC v.1500 64 bit (AMD64)]
>>> from __future__ import print_function
>>> print(1, end=',')
1,
Answered By: Teddy

when you use eclipse editor,just typed like this,

from __future__ import print_function

for i in range(0,5):
    for j in range(0,i):
        print(j, end='')
    print()
Answered By: kajal siyat

If you’re running it from the command line you may also just need to use the python3 command instead of just python command such as:

python3 MyFile.py
Answered By: Christopher Adams

If you are sure you have Python 3+ intalled, type this as first line in your .py file:

#!/usr/bin/python3.x  <<----- where x is your installed version. 

Example: I start the file with: #!/usr/bin/python3.5
(because I have 3.5 installed)

Answered By: user8143631

I was having the same problem with Eclipse and IntelliJ IDEA, just solved it on Eclipse. Preferences with PyDev–interpreter had added Python 3.6 but the native Python on my Mac OS X was above the new install of the Python 3.6. So simply moving up the 3.6 in the interpreter solved my problem.

End result

Answered By: John G.

Had the same issue while using Python 2.7 and 3.x parallel although 3.x is set as default.
Because my PyInstaller from Python 3.x was removed (don’t know why) the other one used.
Reinstall PyInstaller solved the problem.
Note that PyInstaller shows the used versions in the beginning of its output:

61 INFO: PyInstaller: 3.5
61 INFO: Python: 2.7.16
Answered By: Chris
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.