spidev

raw JSON →
3.8 verified Mon Apr 27 auth: no python

Python bindings for Linux SPI access through spidev. Current version 3.8 provides low-level control of SPI devices via the Linux spidev kernel interface. Release cadence is infrequent; maintained on GitHub.

pip install spidev
error OSError: [Errno 2] No such file or directory
cause SPI device node does not exist; kernel module not loaded or incorrect bus/device number.
fix
Load the spidev module: 'sudo modprobe spidev' and check /dev for spidev files (e.g., ls /dev/spi*).
error PermissionError: [Errno 13] Permission denied
cause User lacks read/write access to /dev/spidev*.
fix
Run as root or add a udev rule to grant permissions (e.g., 'SUBSYSTEM=="spidev", MODE="0666"').
error AttributeError: module 'spidev' has no attribute 'SpiDev'
cause Incorrect import: using 'import spidev' instead of 'from spidev import SpiDev'.
fix
Use 'from spidev import SpiDev' to import the class.
error TypeError: function takes at most 2 arguments (3 given)
cause Wrong number of arguments when calling 'open_path' — expects a single path string.
fix
Use 'spi.open_path("/dev/spidev0.0")' instead of 'spi.open_path(bus, device)'.
gotcha Bits per word default is 8. Changing to other values (e.g., 16) may cause unexpected transfer behavior unless hardware supports it.
fix Set 'spi.bits_per_word = 8' explicitly, or check device datasheet before changing.
breaking In v3.8, the 'open_path' method was added. Older code using only 'open(bus, device)' still works, but dynamic bus allocation may require file path-based opening.
fix Use 'spi.open_path("/dev/spidev0.0")' for dynamic bus numbering, or stick with 'spi.open(bus, device)' for static.
deprecated Using 'spi.xfer2' is deprecated in favor of 'spi.xfer'. xfer2 was a non-standard name and may be removed in future versions.
fix Replace 'spi.xfer2' with 'spi.xfer'.
gotcha Running without root or proper permissions (e.g., no udev rule) yields 'PermissionError' or 'OSError: [Errno 13] Permission denied'.
fix Run as root, or add a udev rule to set permissions on /dev/spidev* (e.g., 'SUBSYSTEM=="spidev", MODE="0666"').

Open SPI bus 0, device 0, transfer bytes, and close.

from spidev import SpiDev

spi = SpiDev()
spi.open(0, 0)  # bus=0, device=0
spi.max_speed_hz = 1000000
spi.mode = 0b00

to_send = [0x01, 0x02, 0x03]
response = spi.xfer(to_send)
print(response)
spi.close()