File tree Expand file tree Collapse file tree 1 file changed +38
-0
lines changed
Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments