pylibiio
pylibiio is the Python binding for the libiio library, which provides a robust interface for interacting with Linux Industrial I/O (IIO) devices. These devices include a wide range of ADCs, DACs, accelerometers, gyros, and various sensors. The library facilitates both local communication on embedded Linux targets and remote communication over USB, Ethernet, or Serial, and is actively maintained by Analog Devices, Inc. The current stable version available on PyPI is 0.25, with frequent updates to the underlying C library.
Common errors
-
ModuleNotFoundError: No module named 'iio' OR Cannot find iio
cause The Python interpreter cannot locate the `iio` module, either because `pylibiio` was not installed, or because the installation path is not in `PYTHONPATH`.fixEnsure `pylibiio` is installed: `pip install pylibiio`. If the issue persists, verify `PYTHONPATH` as described in warnings, especially on Linux or when installing from source. On Windows, ensure the `libiio` C library driver is also correctly installed. -
ImportError: undefined symbol: iio_get_backends_count (or similar 'undefined symbol' error)
cause This error typically occurs when the `pylibiio` Python bindings are compiled or linked against a different version of the `libiio` C library than what is currently installed or accessible at runtime. The specific symbol missing (e.g., `iio_get_backends_count`) indicates a version mismatch.fixEnsure the `libiio` C library version and `pylibiio` Python binding version are in sync. For `pylibiio 0.25`, the `libiio` C library should also be in the `0.x` series, ideally `0.25` or `0.26`. Uninstall and reinstall both if necessary, making sure to build or install `libiio` from a compatible release. -
TypeError: 'builtin_function_or_method' object is not subscriptable (especially on Windows when importing pyadi-iio/pylibiio)
cause This error can occur on Windows due to an issue with `libiio`'s internal handling or specific Python environment configurations, often related to dependency resolution.fixTry reinstalling `pylibiio` from `conda-forge` if using Anaconda (`conda install -c conda-forge pylibiio`), or reinstall the official `libiio` Windows driver from Analog Devices' GitHub releases page.
Warnings
- breaking The `pylibiio` Python bindings (version 0.x) are designed for compatibility with the `libiio` C library versions 0.x. Using `pylibiio` 0.x with `libiio` v1.x (a newer, in-development major version) can lead to runtime errors or unexpected behavior due to API changes.
- gotcha On Linux, if `pylibiio` is installed from source (rather than pip), or if `libiio`'s Python bindings are not in a standard path, you may encounter `ModuleNotFoundError` or other import issues unless `PYTHONPATH` is correctly configured.
- deprecated Past releases of `pylibiio` (e.g., 0.23, 0.21.1) were 'yanked' from PyPI due to critical issues like a broken `find_channel` method or mismatches with required `libiio` C library versions. While these versions are no longer directly installable via `pip`, it highlights the importance of using the latest stable `pylibiio` version and matching `libiio` C library.
Install
-
pip install pylibiio
Imports
- iio
import iio
- Context
from iio import Context
import iio ctx = iio.Context('local') - Device
import iio ctx = iio.Context('local') dev = ctx.find_device('some_device_name')
Quickstart
import iio
import os
# Try to connect to a local or remote IIO context
# Use 'local' for devices on the same machine, or 'ip:address' for remote
# For testing without hardware, you can often use 'dummy'
# The URI can also be 'usb', 'serial', or an IP address (e.g., 'ip:192.168.1.100')
# For robust examples, use an environment variable or default to local
uri = os.environ.get('IIO_URI', 'local')
try:
ctx = iio.Context(uri)
print(f"Connected to IIO context: {ctx.description}")
# Scan for available devices
print("\nAvailable IIO devices:")
for dev in ctx.devices:
print(f" - Device: {dev.name} (ID: {dev.id})")
for channel in dev.channels:
print(f" - Channel: {channel.name} (ID: {channel.id}, Output: {channel.output})")
# Example: Try to read an attribute if it exists
try:
sample_rate = channel.attrs['sampling_frequency'].value
print(f" Sampling Frequency: {sample_rate}")
except KeyError:
pass
except Exception as e:
print(f" Error reading attribute: {e}")
# Example: Find a specific device (replace with a known device on your system)
# For a dummy device, you might use 'iio:device0'
target_device_name = os.environ.get('IIO_TARGET_DEVICE', 'iio:device0')
device = ctx.find_device(target_device_name)
if device:
print(f"\nFound target device: {device.name}")
# Example: Set a device attribute (e.g., gain, if supported)
# try:
# if 'gain_control' in device.attrs:
# device.attrs['gain_control'].value = 'manual'
# print(f"Set gain_control on {device.name}")
# except KeyError:
# pass
# except Exception as e:
# print(f"Error setting device attribute: {e}")
# Example: Enable a channel and create a buffer to read data
input_channel = device.find_channel('voltage0', is_output=False)
if input_channel:
input_channel.enabled = True
print(f"Enabled channel: {input_channel.name}")
buffer = iio.Buffer(device, 4096) # 4096 samples
print(f"Created buffer for {device.name}")
# Read data (replace with actual processing)
data = buffer.read()
print(f"Read {len(data)} bytes from buffer.")
buffer.close()
else:
print(f"Channel 'voltage0' not found on {device.name}.")
else:
print(f"Target device '{target_device_name}' not found.")
ctx.close()
print("\nDisconnected from IIO context.")
except Exception as e:
print(f"Failed to connect to IIO context or interact with devices: {e}")
print("Ensure the libiio C library is installed and IIO devices are available/accessible.")