Syntaxerror: multiple exception types must be parenthesized

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 comtypes

Conclusion

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.

We are hoping that this article helps you fix the error. Thank you for reading itsourcecoders 😊

Leave a Comment