SyntaxError: Missing parentheses in call to 'print'
1 · The lesson
readWhat this error means
The code was written for Python 2, and you are running it with Python 3. In Python 2, print was a statement and needed no brackets: print "Hello". In Python 3, print is a normal function, so the text you want printed goes inside round brackets: print("Hello"). Python 2 has not been supported since January 2020, but a lot of old tutorials, books and answers still show the old style. Python spots this before running anything.
When you see it
File "main.py", line 1 print "Hello, world" ^^^^^^^^^^^^^^^^^^^^ SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?
exec changed in the same way, and gives the same kind of message:
File "main.py", line 1 exec "x = 1" ^^^^^^^^^^^^ SyntaxError: Missing parentheses in call to 'exec'. Did you mean exec(...)?
Why it happens
1. An old tutorial or book. Anything written before about 2015 may use Python 2. Other signs that code is Python 2: raw_input(), xrange(), print "a", b with a trailing comma, except ValueError, e:, and dict.has_key().
2. An old script from work or from a repository that nobody ported to Python 3.
3. Habit from another language. Some languages let you call a function without brackets. Python 3 doesn't.
name = "Ada" print "Hello", name
How to fix it
Put brackets around what you print:
name = "Ada" print("Hello, world") # Hello, world print("Hello", name) # Hello Ada
Translate the other Python 2 print forms. A trailing comma in Python 2 meant "no new line". In Python 3 that is end=" ". Printing to a file used >>; now it is file=:
import sys print("Loading", end=" ") print("done") # Loading done print("warning: low disk", file=sys.stderr)
Fix the rest of the old code at the same time. When print is the problem, the file usually has other Python 2 lines too:
| Python 2 | Python 3 |
|---|---|
print "a", b | print("a", b) |
print "a", | print("a", end=" ") |
print >>f, "a" | print("a", file=f) |
raw_input("Name? ") | input("Name? ") |
xrange(10) | range(10) |
except ValueError, e: | except ValueError as e: |
d.has_key("a") | "a" in d |
exec "code" | exec("code") |
For a long file, a careful search and replace of print (with the space) gets most of the lines. The old 2to3 tool that did this conversion was removed in Python 3.13, so for newer versions do it by hand or with your editor.
Prefer a newer tutorial if a course has many of these. Code written for Python 3 will also show you f-strings like print(f"Hello {name}"), which are the normal way to build text today.
When you'd see this in real code
- An exercise from an older edition of a Python book, or a university course page that was never updated.
- An old Stack Overflow answer, from before about 2015, copied into a new project.
- A build or install script that someone ran with
python3after the system dropped Python 2. pip installof a very old package fails with this error inside itssetup.py, because the package itself was never updated. Look for a maintained alternative.
Related errors
- SyntaxError: invalid syntax: the general message for other old-style lines.
except ValueError, e:gets its own wording,multiple exception types must be parenthesized. - NameError: name 'X' is not defined:
raw_inputandxrangedon't exist in Python 3, so they fail with this. - AttributeError: 'dict' object has no attribute 'X':
has_key()anditeritems()from Python 2 end up here. - AttributeError: 'str' object has no attribute 'X':
.decode()on text, another Python 2 habit.
See Also
- All Python errors: the full index, by type and by when it happens.
- Input and output
- f-strings cheat sheet
Found this useful? Share it with a friend who’s learning Python.