HOWTO · Python
How to Play Mp3 File Using Python
Learn five ways to play an MP3 file with Python, with setup, path, and platform guidance.
On this page
To play an MP3 file with Python, use playsound for a short playback script, pygame for mixer controls, or python-vlc when VLC is available. Python can also ask a browser handler to open the file with webbrowser, or launch an installed system player. In each example, replace sample.mp3 with the path to your audio file. Relative paths are resolved from the process’s current working directory.
Play an MP3 File with playsound
Install the third-party playsound package with python -m pip install playsound. Then call its playback function with the file path. It supports common audio formats including MP3 and WAV; playback depends on the package’s platform audio backend.
This complete script plays sample.mp3 from the current working directory. Install the package first and place the file there, or change track to the correct path. Playback requires a working audio device and a platform audio backend supported by the package.
from pathlib import Path
track = Path("sample.mp3")
if track.is_file():
from playsound import playsound
playsound(str(track))
Play an MP3 File with pygame
Install pygame with python -m pip install pygame. Its mixer API loads the track and starts playback. MP3 decoding depends on the pygame and SDL_mixer build in use, so a WAV file is a practical fallback if the installed build cannot decode your MP3. See how to convert an MP3 file to WAV.
This script loads sample.mp3 from the current working directory. Install pygame first and provide an audio backend. The mixer play() call starts playback asynchronously; the loop keeps the script alive until the track finishes. MP3 decoding depends on the pygame and SDL_mixer build in use, so a WAV file is a practical fallback if the installed build cannot decode the track.
from pathlib import Path
track = Path("sample.mp3")
if track.is_file():
import time
import pygame
pygame.mixer.init()
pygame.mixer.music.load(str(track))
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
time.sleep(0.1)
Play an MP3 File with python-vlc
python-vlc is the pip distribution name; its Python import is vlc. Install the binding with python -m pip install python-vlc. The binding uses VLC’s native media framework, so install a compatible VLC runtime as well and ensure it is available to the process. The required setup can vary by operating system.
This script uses the sample.mp3 file in the current working directory. Install both the python-vlc binding and a compatible VLC runtime before running it. player.play() begins playback asynchronously, so the prompt keeps the process alive while the track plays; press Enter when you want to stop it. Audio output is also required.
from pathlib import Path
track = Path("sample.mp3")
if track.is_file():
import vlc
player = vlc.MediaPlayer(str(track))
player.play()
input("Press Enter to stop playback.\n")
player.stop()
Open an MP3 File with webbrowser
webbrowser is part of Python’s standard library, so it needs no pip installation. This complete example asks the system’s configured browser to open a file URL for sample.mp3 in the current working directory. Resolving the path and converting it to a URI handles spaces and other path characters.
from pathlib import Path
import webbrowser
track = Path("sample.mp3")
if track.is_file():
webbrowser.open(track.resolve().as_uri())
This asks the browser or registered handler to open the file. It does not guarantee that a browser tab will play the audio; behavior depends on the browser and operating system.
Launch a System Player from Python
Use subprocess.run with an argument list to start an installed command-line player. This avoids building a shell command from the file path. Install the player separately; availability and command names depend on the operating system. The examples below use mpg123 on Linux and afplay on macOS.
This complete script uses mpg123 on Linux or afplay on macOS to play sample.mp3 from the current working directory. Install the selected player separately. The argument-list form passes the path as one argument even when it contains spaces. check=True reports a nonzero player exit as a Python error, which is useful when diagnosing a failed launch.
from pathlib import Path
import subprocess
import sys
track = Path("sample.mp3")
if track.is_file():
if sys.platform.startswith("linux"):
player = "mpg123"
elif sys.platform == "darwin":
player = "afplay"
else:
raise SystemExit("Choose a player command for this operating system.")
subprocess.run([player, str(track)], check=True)
For a simple path check before choosing a playback method, this standalone Python example prints the filename from a path in the current working directory:
from pathlib import Path
track = Path("sample.mp3")
print(track.name)
sample.mp3