r/learnpython • u/itsmebo12 • 1d ago
Is there any way to play audio in Python without using external libraries or winsound?
I wanted to play audio in Python, so I started looking for modules that were already installed, it means ones that wouldn't require an internet connection to install (for example Pygame)
I found winsound but the problem is that it will only play audio on Windows and no in other OS
It seemed more convenient not to have to install external libraries to test Python capabilities within the existing environment (such as using Tkinter instead of game engines)
Unfortunately many audio libraries and modules were removed in 3.13 (i'm using 3.14)
11
Upvotes
14
u/_mturtle_ 1d ago
you can with ctypes and direct native calls
15
u/Atmosck 1d ago
If you want to ship something that does stuff like this but doesn't require the user to install any dependencies, python is not the right language because it's interpereted.
6
u/SCD_minecraft 1d ago
Not really, at least not arbitrary sound
What's wrong with pip it in? User needs to download your code anyway, why not also download some extra lib?
5
u/Educational_Virus672 1d ago edited 1d ago
best bet
but window is visible so, it is hard to use it properly
5
u/Diapolo10 I write code for a living -- https://github.com/Diapolo10 1d ago
The short answer is "no", although technically you can run
print('\a')to get a "bell sound". Not that it helps you play audio files.4
u/LayotFctor 1d ago
Use sys.platform to check platform, then subprocesses to call os packages. Linux is quite fragmented between distros, so you have to check whether the audio package you need is even installed.
4
u/Educational_Fold1428 1d ago
I went down the same road when I was messing about sorting music lists, and ended up just playing the file in VLC while the script ran. If you only need to check a wav is what you think it is the wave module reads it fine, but actual playback is where it gets annoying.
2
u/gmes78 1d ago
No. You need to interact with OS APIs to play audio. Unless you want to invoke the C APIs yourself using
ctypes(which you really shouldn't do), you need third-party libraries for that.2
u/Free-Good9203 1d ago
python 3.13 dropped the legacy stdlib audio modules under pep 594 (`ossaudiodev`, `aifc`, `sunau`, `sndhdr`), so aside from `winsound` on windows and `wave` for reading or writing raw wav bytes, there is no built-in cross-platform audio player left in the stdlib. for a truly zero-install script that plays wav files across platforms, branching on `sys.platform` works well: `winsound.PlaySound` on windows, `afplay` via `subprocess` on macos, and checking `shutil.which` for `pw-play`, `paplay`, or `aplay` on linux. if your constraint is only 'no internet on the target machine' rather than strictly stdlib-only, you can also run `pip download -d wheels sounddevice` once online and install offline with `pip install --no-index --find-links wheels sounddevice`.
2
u/cdcformatc 1d ago
there's that one character that when you print it it plays the chime sound. that's pretty much the only audio you can play without a library