SyntaxError: '(' was never closed
1 · The lesson
readWhat this error means
You opened a bracket ((, [ or {) and Python never found the one that closes it. The message names the bracket type, so you will also see '[' was never closed and '{' was never closed. The arrow points at the opening bracket that has no closing bracket. This makes it one of the easier syntax errors to fix: the line it names is usually the line to fix.
In Python 3.9 and earlier the same mistake produced a much less helpful message, SyntaxError: unexpected EOF while parsing ("EOF" is end of file), or a bare invalid syntax on the line after the real problem. If an older tutorial shows either of those for a missing bracket, this is the modern version of it.
When you see it
File "main.py", line 2 total = sum(orders ^ SyntaxError: '(' was never closed
File "main.py", line 1 user = {"name": "Linus", "plan": "pro" ^ SyntaxError: '{' was never closed
A similar error appears when a bracket is closed, but with the wrong kind of bracket:
File "main.py", line 2 cheapest = min(prices] ^ SyntaxError: closing parenthesis ']' does not match opening parenthesis '('
Python calls all three bracket types "parenthesis" in these messages. Read it as "you closed a ( with a ]".
Why it happens
Inside brackets, Python lets an expression continue over as many lines as you want. This is useful: it is how long function calls and dictionaries are written. But it means a missing ) doesn't fail on its own line. Python continues reading, treats the next lines as part of the same expression, and only complains when that becomes impossible.
The common causes:
- Nested calls where one closer went missing.
print(f"Total: {sum(prices)}"has three opening brackets in a row, and it's easy to count only two. - A long dictionary or list written over several lines, with the last
}or]deleted while editing. - Mixed-up closing brackets in nested structures:
["tea", "cake"}ormin(prices]. - Copying part of a multi-line call from somewhere else and leaving its end behind.
When the error line is the wrong line
Python 3.13 is good at pointing to the opener, but in nested structures the reported line can be several lines below the mistake. Here the missing ] belongs on line 3:
customer = {
"name": "Grace",
"orders": [101, 102,
}File "main.py", line 4 } ^ SyntaxError: closing parenthesis '}' does not match opening parenthesis '[' on line 3
The File line says 4, because that is where Python noticed. The message itself says "on line 3", because that is where the unclosed [ lives. Use the second number.
How to fix it
Go to the bracket the arrow points at and find where its expression should end. Then close it there. Don't automatically add it at the end of the line, because the right place may be in the middle:
orders = [120, 85, 40] total = sum(orders) print(f"Total: {total}")
For nested calls, count from the inside out. Each function call needs its own ):
prices = [3.50, 4.25, 2.00] print("Cheapest:", min(prices)) print(f"Total: {sum(prices):.2f}")
For multi-line structures, put each closing bracket on its own line, lined up with the line that opened it. Then a missing one is easy to see:
customer = {
"name": "Grace",
"orders": [101, 102],
"address": {
"city": "Leeds",
"postcode": "LS1 4AP",
},
}
print(customer["address"]["city"], len(customer["orders"]))The trailing commas are deliberate. They let you add or reorder lines without editing the line above, and editing that line is where many bracket mistakes start.
Let the editor help. Every common editor highlights the matching bracket when the cursor is next to one. Click on the opening bracket from the error and see where its closing bracket is, or whether it has one. VS Code also colours bracket pairs by depth, so a closing bracket in the wrong colour is in the wrong place.
If you are on Python 3.9 or older and see unexpected EOF while parsing, look for the last bracket you opened in the file. Upgrading gets you the clearer message on this page.
When you'd see this in real code
- Wrapping an existing expression in
round(...)orint(...)and adding the opener without the closer. - Building a JSON payload for an API call as a nested dict, and losing one
}in a twenty-line literal. - Writing a long
print(f"...")with function calls inside the braces. - Merging two branches where each side edited a different line of the same multi-line function call.
Related errors
- SyntaxError: invalid syntax: what older Pythons said for many of the same mistakes.
- SyntaxError: unterminated string literal: the same mistake with a quote instead of a bracket.
- IndentationError: unexpected indent: what you get when you split a line without brackets.
See Also
- All Python errors: the full index, by type and by when it happens.
- Dictionaries (tutorial)
- Functions (tutorial)
- f-strings
Found this useful? Share it with a friend who’s learning Python.