If you have recently upgraded to Python 3.13 and tried to run an audio manipulation script using pydub (e.g., from pydub import AudioSegment), you probably encountered this frustrating error:
🚨 The Error Message
Plaintext
Traceback (most recent call last):
File "C:\AppData\Local\Programs\Python\Python313\Lib\site-packages\pydub\utils.py", line 14, in <module>
import audioop
ModuleNotFoundError: No module named 'audioop'
During handling of the above exception, another exception occurred:
...
ModuleNotFoundError: No module named 'pyaudioop'
Why does this happen in Python 3.13, and how can you fix it without downgrading your Python version? Here is the exact cause and a robust, modern solution.
❓ The Cause: ‘audioop’ Module Removed in Python 3.13
According to the official Python documentation, the legacy built-in module audioop has been permanently removed starting from Python 3.13.
The problem is that many popular audio processing libraries, most notably pydub, still internally rely on this deprecated audioop module to handle raw audio data. As a result, importing or running pydub in Python 3.13 triggers a fatal ModuleNotFoundError.
🛠️ The Solution: Modern Audio Normalization using pedalboard and numpy
While downgrading to Python 3.12 is an option, it is not ideal. A much better and more modern approach is to bypass pydub entirely and use Spotify’s high-performance pedalboard library combined with numpy for mathematical audio processing.
This approach is fully compatible with Python 3.13 and offers much faster and more accurate audio processing.
Step 1: Install the Required Libraries
Open your terminal and install the Python 3.13-compatible audio packages:
PowerShell
pip install pedalboard numpy
Step 2: Python 3.13 Compatible Audio Normalization Script
Below is the complete, optimized Python script to normalize multiple MP3 files in a directory to -14.0 dB (the YouTube/Streaming standard loudness) without any audio clipping:
Python
import os
import numpy as np
from pedalboard.io import AudioFile
# Directory containing your audio files
AUDIO_DIR = "audio_pool"
# YouTube standard loudness target
TARGET_DB = -14.0
def normalize_audio():
# Get all MP3 files in the directory
files = [f for f in os.listdir(AUDIO_DIR) if f.endswith(".mp3")]
if not files:
print("💡 No MP3 files found in the directory.")
return
print(f"🚀 [Python 3.13 Engine] Starting normalization for {len(files)} files...\n")
for file_name in files:
file_path = os.path.join(AUDIO_DIR, file_name)
temp_path = file_path + ".tmp.wav" # Temporary file for safe writing
try:
# 1. Read the audio file safely
with AudioFile(file_path) as f:
audio_data = f.read(f.frames)
sr = f.samplerate
num_channels = f.num_channels
# 2. Calculate RMS and current Decibels (dB) using NumPy
rms = np.sqrt(np.mean(audio_data**2))
current_db = 20 * np.log10(rms + 1e-10)
# 3. Calculate gain to reach the target dB
gain_db = TARGET_DB - current_db
gain = 10 ** (gain_db / 20)
adjusted_audio = audio_data * gain
# 4. Prevent clipping/distortion
max_val = np.max(np.abs(adjusted_audio))
if max_val > 0.95:
adjusted_audio = (adjusted_audio / max_val) * 0.95
# 5. Write to a temporary file and replace the original
with AudioFile(temp_path, 'w', sr, num_channels) as f:
f.write(adjusted_audio)
os.remove(file_path)
os.rename(temp_path, file_path)
print(f"✅ Success: {file_name} ({current_db:.1f} dB -> {TARGET_DB} dB)")
except Exception as e:
if os.path.exists(temp_path):
os.remove(temp_path)
print(f"❌ Error processing {file_name}: {e}")
print("\n🎉 Audio normalization complete!")
if __name__ == "__main__":
normalize_audio()
💡 Summary
- Issue: Python 3.13 removed
audioop, breakingpyduband causing aModuleNotFoundError. - Solution: Replace
pydubwithpedalboardandnumpyto process audio mathematically. This keeps your pipeline fast, modern, and fully compatible with Python 3.13+.
If you are building Discord bots, automated YouTube stream engines, or audio processing tools, this workaround will save you from having to downgrade your Python environment.