pythonpython-2.7pyudev

pyudev type object 'Context' has no attribute '_libudev'


I am using the pyudev library on Debian GNU/Linux 8.7 (jessie) with python2.7 to detect USB devices as follows:

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()

As shown in multiple examples - however when I run the code I get the following error:

/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'

Originally after installing pyudev via pip I forgot to ensure I had libudev-dev installed so I installed libudev-dev, uninstalled pyudev and reinstalled it via pip, but the error persists.

I am currently running libudev-dev version 215

Can anyone advise why this error may be occurring and how to potentially fix it? I have taken a look through their Github issues but have not found anyone having the same issue I've also take a look at their Read the Docs wiki and still no luck.


Solution

  • It seems you need to instantiate the Context to use it, so add parantheses:

    context = pyudev.Context()
    

    Then filter_by requires another input argument. But if you look at the docs you could probably figure it out.