winsound
is a Python module specifically designed for Windows, and it is not available on Linux systems. This is because it relies on the Windows API for sound playback, which doesn’t exist on Linux.
For Linux, you’ll need to use alternative libraries for playing sound. Here are a few options along with the commands to install them:
#1 – Pygame
π΅ Pygame: A popular module that can handle various media, including sound.
The best way to install pygame
is to use pip with the --user
flag to tell it to install into the home directory, rather than globally.
python3 -m pip install -U pygame --user
To check if it works, run one of the prebuilt examples:
python3 -m pygame.examples.aliens
Their official page is available here. Here’s a minimal example:
import pygame pygame.init() pygame.mixer.init() sound = pygame.mixer.Sound('your_sound_file.wav') sound.play() pygame.event.wait() # Wait for the sound to finish playing
#2 – Simpleaudio
π΅ Simpleaudio: A straightforward library for playing WAV files.
pip install simpleaudio
A minimal quick start would be this:
import simpleaudio as sa wave_obj = sa.WaveObject.from_wave_file('your_sound_file.wav') play_obj = wave_obj.play() play_obj.wait_done() # Wait for the sound to finish playing
#3 – Pydub
π΅ Pydub: A higher-level library that can handle different audio formats, but requires additional dependencies for format conversion.
pip install pydub
To handle audio formats other than WAV, you’ll also need ffmpeg
or libav
. Install it using your Linux distribution’s package manager, for example:
# For Debian/Ubuntu sudo apt-get install ffmpeg # For Fedora sudo yum install ffmpeg # For Arch Linux sudo pacman -S ffmpeg
A minimal quick start would be this:
from pydub import AudioSegment from pydub.playback import play # Can be mp3, wav, etc. sound = AudioSegment.from_file('your_sound_file.mp3') play(sound)
#4 – Playsound
π΅ Playsound: A pure Python, cross-platform, single function module with no dependencies.
pip install playsound
A minimal quick start would be this:
from playsound import playsound playsound('your_sound_file.mp3')
Each of these libraries has its own API and way of handling audio. You will need to refer to their respective documentation for usage instructions.
Additionally, ensure you have Python’s package manager pip
installed before running these commands. If it’s not installed, you can usually install it on Linux with a command like sudo apt-get install python3-pip
(for Debian/Ubuntu-based distributions).
Just for fun, I have created a tutorial that shows how to use Winsound:
π§βπ» Recommended: How to Make a Beep Sound in Python? [Linux/macOS/Win]