如何修复module 'fluidsynth' has no attribute 'Synth'报错
执行
pm.fluidsynth()时出现"module 'fluidsynth' has no attribute 'Synth'"报错的解决思路 问题场景
在跟随TensorFlow音乐生成教程时,运行代码waveform = pm.fluidsynth(fs=_SAMPLING_RATE)触发报错:module 'fluidsynth' has no attribute 'Synth'。使用PyCharm,所有依赖包安装在单个虚拟环境中,已尝试以下方案但未解决:
- 安装了
pip install pyfluidsynth(版本1.23.5)和pip install fluidsynth(版本0.2); - 尝试了Stack Overflow上的旧版本解决方案,但无效;
- 执行
print(fluidsynth.__version__)时,提示module 'fluidsynth' has no attribute '__version__'。
解决步骤
清理冲突包
同时安装pyfluidsynth和fluidsynth会导致命名冲突,先彻底卸载两个包:pip uninstall -y pyfluidsynth fluidsynth仅重新安装
pyfluidsynth:pip install pyfluidsynth==1.23.5验证包的可用性
在PyCharm的虚拟环境终端中运行以下代码,确认pyfluidsynth的Synth类可正常访问:import pyfluidsynth as fluidsynth print(fluidsynth.__version__) synth = fluidsynth.Synth()若这段代码无报错,说明包本身正常,问题出在
pretty_midi的调用逻辑上。修正pretty_midi的fluidsynth绑定
pretty_midi默认尝试导入原生fluidsynth包,可手动强制它使用pyfluidsynth:
在调用pm.fluidsynth()前添加如下代码:import pyfluidsynth import pretty_midi as pm pm.pretty_midi.fluidsynth = pyfluidsynth检查系统级依赖
pyfluidsynth依赖系统级的FluidSynth库,需确保系统已安装:- Windows:下载FluidSynth二进制包,解压后将包含
fluidsynth.dll的目录添加到系统环境变量PATH; - macOS:通过Homebrew执行
brew install fluidsynth; - Linux(Debian/Ubuntu):执行
sudo apt-get install fluidsynth。
- Windows:下载FluidSynth二进制包,解压后将包含
内容的提问来源于stack exchange,提问作者Xaver
相关产品推荐
相关产品推荐

