pyudev类型对象'Context'没有属性'_libudev'

时间:2017-01-28 22:58:00

标签: python python-2.7 pyudev

我正在使用Debian GNU/Linux 8.7 (jessie)上的pyudevpython2.7来检测USB设备,如下所示:

import sys
import pyudev

def main():
    os = canary.helpers.get_platform_system()

    if os.lower() == "linux":
        print("linux")
        context = pyudev.Context

        monitor = pyudev.Monitor.from_netlink(context)
        monitor.filter_by(device_type='usb')

    elif os.lower() == 'darwin': # actually OS X
        print("OS X is currently not supported, if you would like to add support make a pull request. Aborting...")
        sys.exit()
    elif os.lower() == 'windows':
        print("Windows is currently not supported, if you would like to add support make a pull request. Aborting...")
        sys.exit()
    else:
        print("Unknown operating system. Aborting...")
        sys.exit()


if __name__ == "__main__":
    main()

如多个示例所示 - 但是当我运行代码时,我收到以下错误:

/usr/bin/python2.7 /home/marvin/src/usb_canary/usb_canary.py
linux
Traceback (most recent call last):
File "/home/marvin/src/usb_canary/usb_canary.py", line 45, in <module>
main()
File "/home/marvin/src/usb_canary/usb_canary.py", line 30, in main
monitor = pyudev.Monitor.from_netlink(context)
File "/usr/local/lib/python2.7/dist-packages/pyudev/monitor.py", line 121, in from_netlink
monitor = context._libudev.udev_monitor_new_from_netlink(
AttributeError: type object 'Context' has no attribute '_libudev'

最初在通过pip安装pyudev之后我忘了确保安装了libudev-dev所以我安装了libudev-dev,卸载了pyudev并通过pip重新安装了它,但错误仍然存​​在

我目前正在运行libudev-dev版本215

任何人都可以建议为什么会出现此错误以及如何进行修复?我已经看了他们的Github问题,但没有发现任何人有同样的问题我也看了他们的Read the Docs维基并且仍然没有运气。

1 个答案:

答案 0 :(得分:0)

似乎你需要实例化Context才能使用它,所以添加parantheses:

context = pyudev.Context()

然后filter_by需要另一个输入参数。但是如果你看看文档,你可能会想出来。