You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用librosa时出现ModuleNotFoundError错误的求助

问题

我尝试用以下代码将音频文件加载到NumPy数组中:

#%%
import librosa
import matplotlib.pyplot as plt
import IPython.display as ipd
import os, os.path
import time
import joblib
import numpy as np

#%%
fname = 'archive\\Actor_01\\03-01-01-01-01-01-01.wav'

data, sampling_rate = librosa.load(fname)
plt.figure(figsize=(15, 5))
librosa.display.waveshow(data, sr=sampling_rate)

ipd.Audio(fname)

# %% 
lst = []

for subdir, dirs, files in os.walk('archive'):
    for file in files:
        try:
            X, sample_rate = librosa.load(os.path.join(subdir, file), res_type='kaiser_fast')
            mfccs = np.mean(librosa.feature.mfcc(y=X, sr=sample_rate, n_mfcc=40).T, axis=0)
            file_class = int(file[7:8]) - 1
            arr = mfccs, file_class
            lst.append(arr)
        except ValueError as err:
            print(err)
            continue

除第25行外其余代码运行正常,报错信息如下:

ModuleNotFoundError                       Traceback (most recent call last)
c:\Users\powellt1\Documents\COMP5500\NonGitSED\algorithm.py in line 7
      23 for file in files:
      24     try:
----> 25         X, sample_rate = librosa.load(os.path.join(subdir, file), res_type='kaiser_fast')
      26         mfccs = np.mean(librosa.feature.mfcc(y=X, sr=sample_rate, n_mfcc=40).T, axis=0)
      27         file_class = int(file[7:8]) - 1
File c:\Users\powellt1\AppData\Local\Programs\Python\Python311\Lib\site-packages\librosa\core\audio.py:193, in load(path, sr, mono, offset, duration, dtype, res_type)
    190     y = to_mono(y)
    192 if sr is not None:
--> 193     y = resample(y, orig_sr=sr_native, target_sr=sr, res_type=res_type)
    195 else:
    196     sr = sr_native

File c:\Users\powellt1\AppData\Local\Programs\Python\Python311\Lib\site-packages\librosa\core\audio.py:684, in resample(y, orig_sr, target_sr, res_type, fix, scale, axis, **kwargs)
    675     y_hat = np.apply_along_axis(
    676         soxr.resample,
    677         axis=axis,
   (...)
    681         quality=res_type,
    682     )
    683 else:
--> 684     y_hat = resampy.resample(y, orig_sr, target_sr, filter=res_type, axis=axis)

已确认librosa正确安装且在之前单元格可正常使用,试过重装librosa、验证安装,也试过from librosa.core import load,都无法解决错误。

解决方案
  • 安装缺失的resampy库:报错核心是librosa调用resampy.resample时找不到模块,执行命令 pip install resampy 即可补全依赖。
  • 替换重采样参数:如果不想安装额外库,可将res_type='kaiser_fast'替换为无需resampy的选项,比如res_type='fft'或res_type='soxr_hq'(后者需要安装soxr,执行pip install soxr)。
  • 检查Python环境一致性:确认当前运行代码的环境和安装librosa的环境是同一个,避免虚拟环境隔离导致的模块缺失。可通过pip list查看当前环境是否包含resampy和librosa。

内容的提问来源于stack exchange,提问作者DaddyMuffin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 18:34:52