Python Mido 库示例不起作用

Python Mido library example does not work

我安装了mido with pip install mido and tried to execute the example code from the docs

import mido

with mido.open_input('SH-201') as inport:
    for message in inport:
        print(message)

但我收到以下错误消息:

  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ctypes/__init__.py", line 365, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: dlopen(libportmidi.dylib, 6): image not found

知道我做错了什么吗?

编辑:有没有其他方便的方法来使用 python 从 MIDI 设备读取输入?

您需要安装一个与 MIDI 硬件接口的后端,例如 PortMidi。错误信息告诉你后端没有找到。

您可以通过 Homebrew 安装它。 在终端中,运行:

ruby -e "$(curl -fsSL   https://raw.githubusercontent.com/Homebrew/install/master/install)" < /dev/null 2> /dev/null

安装 Homebrew。然后,运行

brew install portmidi

安装 PortMidi。

下载 pmdefaults Mac binary,将您的 MIDI 设备连接到计算机,然后 运行 pmdefaults(right-click、select 打开,因为应用程序未签名)。 Select 您的默认 MIDI 设备,单击“更新首选项”,然后退出应用程序。启动一个新的 Python 解释器。 Mido 现在应该可以正常工作了。