Today, we are going to deal with syntaxerror: multiple exception types must be parenthesized error message in Python.
If you want to get rid of this error right now? Then, continue reading!
In this article, we’ll walk you through how to troubleshoot the multiple exception types must be parenthesized a Syntax Error in Python.
What is “syntaxerror multiple exception types must be parenthesized”?
The error message syntaxerror multiple exception types must be parenthesized occurs when you are using an older syntax for handling exceptions that are no longer valid in newer versions of Python.
For example:
try:
a = 10 / 0
except ZeroDivisionError, err:
print("Error:", err)
When you try to run this code, the output would be:
File "C:\Users\pies-pc2\PycharmProjects\pythonProject\main.py", line 3
except ZeroDivisionError, err:
^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: multiple exception types must be parenthesized
How to fix the “syntaxerror: multiple exception types must be parenthesized”?
To fix the syntaxerror multiple exception types must be parenthesized error, you have to update the syntax for handling exceptions in your code. Instead of writing except COMError, err:, you should write except COMError as err:.
This change in syntax is required in newer versions of Python.
Update the except statement to use the new syntax like this
In the past, you could write:
except COMError, err:However, a newer version of Python you should write:
except COMError as err:For example:
Incorrect code
try:
a = 10 / 0
except ZeroDivisionError, err:
print("Error:", err)In this example, the first try block uses the old syntax for handling exceptions and results to syntax error: multiple exception types must be parenthesized error.
Corrected code:
try:
a = 10 / 0
except ZeroDivisionError as err:
print("Error:", err)
The second try block uses the new syntax for handling exceptions and does not result in an error.
Output:
Error: division by zero
When the above solution does not resolve the error you can try to downgrade two packages and reinstall comtypes.
It is because comtypes uses a tool to be compatible with both python 2 and 3 and that is no longer works in new versions.
You can try to execute the following command to downgrade two packages and reinstall comtypes:
✅pip install setuptools==57.0.0 --force-reinstall
✅pip install wheel==0.36.2 --force-reinstall
✅ pip uninstall comtypes
✅pip install --no-cache-dir comtypesConclusion
In conclusion, the error message syntaxerror multiple exception types must be parenthesized occurs when you are using an older syntax for handling exceptions that are no longer valid in newer versions of Python.
To fix this error, you have to update the syntax for handling exceptions in your code. Instead of writing except COMError, err:, you should write except COMError as err:.
By executing the solutions above, you can master this SyntaxError with the help of this guide.
You could also check out other SyntaxError articles that may help you in the future if you encounter them.
- Uncaught syntaxerror: invalid shorthand property initializer
- Expression.syntaxerror: token comma expected.
- Uncaught syntaxerror unexpected token react
We are hoping that this article helps you fix the error. Thank you for reading itsourcecoders 😊
Frequently Asked Questions
Official documentation
What is Python SyntaxError and what causes it?
SyntaxError is raised when Python’s parser can’t understand your code. Common causes: missing colon (def foo() instead of def foo():), unmatched parentheses or brackets, incorrect indentation, mixing tabs and spaces, missing comma in a list, or using a Python 3 feature in a Python 2 interpreter (or vice versa). The error points to a line, but the actual issue is often on the line BEFORE.
How do I fix ‘unexpected EOF while parsing’?
Python ran out of code while expecting more, usually unclosed parenthesis, bracket, brace, or quote. Scroll up from the error line and count opening vs closing pairs. Use an IDE with bracket matching (VS Code, PyCharm) to highlight pairs. Common gotcha: a triple-quoted string that’s missing its closing triple-quote.
Why does my syntax error point to a line that looks correct?
The actual error is often on the line BEFORE the one Python reports. Python reads code until something doesn’t parse, then reports the position where it gave up, not where the user’s mistake started. Check the line above the reported error for missing colons, commas, or closing brackets.
Why does ‘print x’ raise SyntaxError in Python 3?
Python 3 made print a function, so print(x) is required. The bare ‘print x’ syntax was Python 2 only. If you’re following an old tutorial, check whether it targets Python 2 (released 2008) or Python 3 (your current interpreter). Update all print statements to print(…) function calls.
Where can I find more SyntaxError fixes?
Browse the SyntaxError reference hub for 48+ specific fixes (Python and JavaScript). For Python fundamentals see the Python Tutorial hub. For JavaScript syntax errors see the JavaScript Tutorial hub.
