Skip to content

Commit 2b8a126

Browse files
authored
fix (#356)
1 parent 25d4457 commit 2b8a126

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from __future__ import annotations
2+
3+
from types import ModuleType
4+
5+
6+
def load_pyaudio() -> ModuleType:
7+
"""
8+
Import PyAudio with a friendly error when PortAudio is missing.
9+
10+
Raises:
11+
RuntimeError: If PyAudio/PortAudio cannot be imported.
12+
"""
13+
try:
14+
import pyaudio
15+
except Exception as exc:
16+
details = str(exc).lower()
17+
if isinstance(exc, ModuleNotFoundError) and exc.name == "pyaudio":
18+
message = (
19+
"PyAudio is required to use the microphone.\n"
20+
"Install PortAudio (eg. for macos: brew install portaudio), then "
21+
"reinstall PyAudio."
22+
)
23+
elif "pyaudio._portaudio" in details or "portaudio" in details:
24+
message = (
25+
"PyAudio is installed, but the PortAudio native library is missing or "
26+
"failed to load.\n"
27+
"Install PortAudio (eg. for macos: brew install portaudio), then "
28+
"reinstall PyAudio."
29+
)
30+
else:
31+
message = (
32+
"PyAudio is required to use the microphone, but it could not be "
33+
"imported.\n"
34+
"Install PortAudio (eg. for macos: brew install portaudio), then "
35+
"reinstall PyAudio."
36+
)
37+
raise RuntimeError(message) from exc
38+
return pyaudio

0 commit comments

Comments
 (0)