SyntaxError: multiple exception types must be parenthesized

57.2k Views Asked by At

I am a beginner and have a problem after installing pycaw for the audio control using python, on putting the basic initialization code for pycaw, i get the following error:-

Traceback (most recent call last):
  File "c:\Users\...\volumeControl.py", line 7, in <module>
    from comtypes import CLSCTX_ALL
  File "C:\...\env\lib\site-packages\comtypes\__init__.py", line 375
    except COMError, err:
           ^^^^^^^^^^^^^
SyntaxError: multiple exception types must be parenthesized

Basic initialization:-

from ctypes import cast, POINTER
from comtypes import CLSCTX_ALL
from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume

devices = AudioUtilities.GetSpeakers()
interface = devices.Activate(
    IAudioEndpointVolume._iid_, CLSCTX_ALL, None)
volume = cast(interface, POINTER(IAudioEndpointVolume))

I tried searching for this all over the web but could not find a fix
I also trying going into the module file inside the virtual env and parenthize by putting brackets around COMError, err
But same error with other lines in code came,
Also tried reinstalling pycaw and trying to install different versions of pycaw several times but nothing fixed

How to fix this error?

4

There are 4 best solutions below

0
Romulo Santos On BEST ANSWER

After a time searching I found that comtypes uses a tool to be compatible with both python 2 and 3 and that is no longer works in new versions. I had 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
4
B K On

I just figured out what the cryptic "SyntaxError: multiple exception types must be parenthesized" message means. All it's trying to tell you is that in the newer version of Python you're using, this syntax is no longer valid:

except COMError, err:

instead, you should be using this syntax:

except COMError(err):
4
Salman Siddiqui On

Try the syntax

except (OSError, e):
2
Ibad Khan On

You should some changes in the Python file.

For example, if you run the sqlalchemy library and here you got the error then you will edit the file of this directory.

  • Locate the exc.py file. In your case, it seems to be in the directory: ~\anaconda3\lib\site-packages\sqlalchemy\exc.py.
  • Open the exc.py file in a text editor.
  • Find the line that contains the except Exception, e: statement.
  • Replace that line with the following code: `except (Exception, e):