Skip to main content

Pure-python wrapper for libusb-1.0

Project description

Pure-python wrapper for libusb-1.0

Supports all transfer types, both in synchronous and asynchronous mode.




  • CPython 2.7+ or 3.4+, pypy 2.0+. Older versions may work, but are not recommended as there is no automated regression testing set up for them.
  • libusb-1.0 or libusbx

Supported OSes

python-libusb1 can be expected to work on:

[1]Beware of libusb-win32, which implements 0.1 API, not 1.0 .
[2]Beware of possible lack of select.poll if you want to use asynchronous API.


Releases from PyPI, with name libusb1. Installing from command line:

$ pip libusb1


$ easy_install libusb1

Latest version from source tree:

$ git clone
$ cd python-libusb1
$ python install


Finding a device and gaining exclusive access:

import usb1
context = usb1.USBContext()
handle = context.openByVendorIDAndProductID(
if handle is None:
    # Device not present, or user is not allowed to access device.

Synchronous I/O:

while True:
    data = handle.bulkRead(ENDPOINT, BUFFER_SIZE)
    # Process data...

Asynchronous I/O, with more error handling:

def processReceivedData(transfer):
    if transfer.getStatus() != usb1.TRANSFER_COMPLETED:
        # Transfer did not complete successfully, there is no data to read.
        # This example does not resubmit transfers on errors. You may want
        # to resubmit in some cases (timeout, ...).
    data = handle.getBuffer()[:transfer.getActualLength()]
    # Process data...
    # Resubmit transfer once data is processed.

# Build a list of transfer objects and submit them to prime the pump.
transfer_list = []
for _ in xrange(TRANSFER_COUNT):
    transfer = handle.getTransfer()
        usb1.ENDPOINT_IN | ENDPOINT,
# Loop as long as there is at least one submitted transfer.
while any(x.isSubmitted() for x in reader_list):
    except usb1.USBErrorInterrupted:

For more, see the example directory.


python-libusb1 main documentation is accessible with python’s standard pydoc command.

python-libusb1 follows libusb-1.0 documentation as closely as possible, without taking decisions for you. Thanks to this, python-libusb1 does not need to duplicate the nice existing libusb1.0 documentation.

Some description is needed though on how to jump from libusb-1.0 documentation to python-libusb1, and vice-versa:

usb1 module groups libusb-1.0 functions as class methods. The first parameter (when it’s a libusb_... pointer) defined the class the fonction belongs to. For example:

  • int libusb_init (libusb_context **context) becomes USBContext class constructor, USBContext.__init__(self)
  • ssize_t libusb_get_device_list (libusb_context *ctx, libusb_device ***list) becomes an USBContext method, returning a list of USBDevice instances, USBDevice.getDeviceList(self)
  • uint8_t libusb_get_bus_number (libusb_device *dev) becomes an USBDevice method, USBDevice.getBusNumber(self)

Error statuses are converted into usb1.USBError exceptions, with status as value instance property.

usb1 module also defines a few more functions and classes, which are otherwise not so convenient to call from Python: the event handling API needed by async API.



Initial release


Massive rework of, making it more python-ish and fixing some memory leaks.


Deprecate “transfer” constructor parameter to allow instance reuse.


Some work on isochronous “in” transfers. They don’t raise exceptions anymore, but data validity and python-induced latency impact weren’t properly checked.


Fix asynchronous configuration transfers.

Stand-alone polling thread for multi-threaded apps.

More libusb methods exposed on objects, including ones not yet part of released libusb versions (up to their commit 4630fc2).

2to3 friendly.

Drop deprecated USBDevice.reprConfigurations method.


Add FreeBSD support.


Add Cygwin support.

OpenBSD support checked (no change).


Add fink and homebrew support on OSX.

Drop PATH_MAX definition.

Try harder when looking for libusb.


Fix FreeBSD ABI compatibility.

Easier to list connected devices.

Easier to terminate all async transfers for clean exit.

Fix few segfault causes.

pypy support.


Descriptor walk API documented.

Version and capability APIs exposed.

Some portability fixes (OSes, python versions).

Isochronous transfer refuses to round transfer size.

Better exception handling in enumeration.

Add examples.

Better documentation.


Wrap hotplug API.

Wrap port number API.

Wrap kernel auto-detach API.

Drop wrapper for libusb_strerror, with compatibility place-holder.

Add a few new upstream enum values.


Backward-incompatible change: Enum class now affects caller’s local scope, not its global scope. This should not be of much importance, as:

  • This class is probably very little used outside

  • This class is probably mostly used at module level, where locals == globals.

    It is possible to get former behaviour by providing the new scope_dict parameter to Enum constructor:

    SOME_ENUM = libusb1.Enum({...}, scope_dict=globals())

Improve start-up time on CPython by not importing standard inspect module.

Fix some more USBTransfer memory leaks.

Add Transfer.iterISO for more efficient isochronous reception.


Fixed USBContext.waitForEvent.

Fix typo in USBInterfaceSetting.getClassTuple method name. Backward compatibility preserved.

Remove globals accesses from USBDeviceHandle destructor.

Assorted documentation improvements.


Made USBDevice instances hashable.

Relaxed licensing by moving from GPL v2+ to LGPL v2.1+, for consistency with libusb1.


Reduce (remove ?) the need to import libusb1 module by exposing USBError and constants in usb1 module.


Fix pydoc appearance of several USBContext methods.

Define exception classes for each error values.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for libusb1, version 1.4.0
Filename, size File type Python version Upload date Hashes
Filename, size libusb1-1.4.0.tar.gz (45.2 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page