Pure Python, cross platform, single function module with no dependencies for playing sounds.
npx @tessl/cli install tessl/pypi-playsound@1.3.0Pure Python, cross platform, single function module with no dependencies for playing sounds. Provides a simple interface for playing audio files across Windows, macOS, and Linux platforms without requiring external audio libraries.
pip install playsoundfrom playsound import playsoundFor exception handling:
from playsound import playsound, PlaysoundExceptionfrom playsound import playsound
# Play a sound file (blocking - waits until playback completes)
playsound('/path/to/your/sound/file.mp3')
# Play a sound file asynchronously (non-blocking)
playsound('/path/to/your/sound/file.wav', block=False)
# With error handling
from playsound import playsound, PlaysoundException
try:
playsound('/path/to/sound.mp3')
except PlaysoundException as e:
print(f"Error playing sound: {e}")playsound uses platform-specific implementations automatically selected at import time:
windll.winmm MCI commands for audio playbackAppKit.NSSound (falls back to subprocess if PyObjC unavailable)gi.repository.Gst) (falls back to subprocess if unavailable)The module handles path formatting, special characters, and provides consistent behavior across Python 2.3+ and Python 3.x versions.
Plays audio files with cross-platform compatibility and automatic platform detection.
def playsound(sound, block=True):
"""
Play a sound file using platform-appropriate audio system.
Args:
sound (str or Path-like): Path to sound file (local path or URL)
block (bool): If True, wait until playback completes; if False, play asynchronously
Returns:
None
Raises:
PlaysoundException: On playback errors, file not found, or audio system failures
"""Supported Formats: MP3, WAV (tested on all platforms), other formats may work depending on platform capabilities
Platform Support:
Custom exception for playsound-specific errors.
class PlaysoundException(Exception):
"""
Exception raised for playsound-specific errors.
Raised when:
- Sound file cannot be found
- Audio format is unsupported
- Platform audio system encounters errors
- Invalid file paths or URLs
"""playsound can be used directly from the command line:
python playsound.py /path/to/sound/file.mp3When used from command line, playback is always synchronous (blocking).
Supported Path Types:
'/path/to/file.mp3'Path('/path/to/file.mp3')'http://example.com/sound.mp3' (platform dependent)Special Character Handling: On Windows, paths containing special characters "\'() are automatically handled by creating temporary file copies.
windll.winmmAppKit.NSSound (requires PyObjC: pip install PyObjC)gi.repository.Gst (requires pip install pygobject)Common scenarios that raise PlaysoundException:
Core Dependencies: None (pure Python)
Optional Dependencies (for optimal performance):
PyObjC - pip install PyObjCpygobject - pip install pygobjectWithout optional dependencies, playsound falls back to subprocess methods with system Python interpreters.