How to Make a Beep Sound in Python? [Linux/macOS/Win]

Rate this post

Windows: How to Make Beep in Python

To make a beep sound in Python on your Windows machine:

  • Import the library using: import winsound
  • Call windsound.Beep(frequency, duration) for your desired frequency (in Hertz) and duration (in milliseconds).
  • For example, winsound.Beep(2000, 1500) would make a sound with 2000 Hz for 1.5 seconds.

Here’s the relevant code snippet for copy and pasting:

import winsound

# Set frequency to 2000 Hertz
frequency = 2000

# Set duration to 1500 milliseconds (1.5 seconds)
duration = 1500

# Make beep sound on Windows
winsound.Beep(frequency, duration)

Application: All My Little Ducklings (“Alle Meine Entchen”) in Python

I used the winsound library to create a small song with beep sounds in Python.

from winsound import Beep

notes = {'C': 1635,
         'D': 1835,
         'E': 2060,
         'S': 1945,
         'F': 2183,
         'G': 2450,
         'A': 2750,
         'B': 3087,
         ' ': 37}


melodie = 'CDEFG G AAAAG AAAAG FFFFE E DDDDC'
for note in melodie:
    Beep(notes[note], 500)

We use a dictionary notes that stores for each note the frequency to be played as a beep sound.

The melodie variable holds the song to be played as a series of notes. For example, the melodie 'ABC' would encode the melodie consisting of three notes 'A', 'B', and 'C'—in this sequence.

We play each note for 500 ms using the Python function call: Beep(notes[note], 500).

You can discover more super fun and interesting sound possibilities for Python on Windows here.

Linux: How to Make Beep in Python

To make a beep sound in Python on your Linux machine:

  • Install the beep library with apt-get install beep in your terminal.
  • Open a Python shell by typing the python command in your terminal.
  • Import the os library using import os in your Python shell.
  • Run the os.system() function and pass the "beep -f 2000 -l 1500" string command to run the beep command on your Linux system with 2000 Hz frequency and 1500 ms duration. You can set the parameters accordingly to your own frequency and sound duration.

Here’s the code:

$ apt-get install beep
$ python
>>> import os
>>> os.system("beep -f 2000 -l 1500")

Instead of os.system() an objectively better way would be to use the subprocess.Popen() function to run a command on your machine.

Beepy Module for macOS, Win, Linux

First install the beepy library from your command line, terminal or shell—depending on which operating system you use:

$ pip install beepy

For more information on how to install a module, please visit these blog posts:

After installation, you can initiate one of seven different sound types from your Python shell using the beep() function of the beepy module.

Consider these different sound types:

  • 1 : 'coin'
  • 2 : 'robot_error'
  • 3 : 'error'
  • 4 : 'ping'
  • 5 : 'ready'
  • 6 : 'success'
  • 7 : 'wilhelm'

You can either pass the given integer or string as sound argument of the beep(sound) function.

For example, those two function calls would generate the same sound:

beep(sound=1)
beep(sound='coin')

This should work for all major operating systems.

ASCII Bell Character

In some instances, you can simply use the ASCII Bell Character '\a' and print it to the standard output. However, it didn’t work on my Windows machine.

print('\a')

Yet, this method should work in the macOS terminal and play the system warning sound.

References: These and some additional sounds can be found here.