{"library":"smbus2","title":"smbus2","type":"library","description":"smbus2 is a pure Python implementation of the `python-smbus` package, designed as a drop-in replacement for `smbus-cffi` or `smbus-python`. It provides an interface for I2C and SMBus communication on Linux-based systems, offering enhanced features like SMBus Packet Error Checking (PEC), support for context managers for robust resource handling, and improved error management. Actively maintained, the library is currently at version 0.6.1, with a regular release cadence addressing bug fixes and minor improvements, making it a recommended choice for embedded I2C communication, particularly on platforms like Raspberry Pi.","language":"python","status":"active","last_verified":"Fri May 15","install":{"commands":["pip install smbus2"],"cli":null},"imports":["from smbus2 import SMBus","from smbus2 import SMBus","from smbus2 import i2c_msg"],"auth":{"required":false,"env_vars":[]},"links":{"homepage":null,"github":"https://github.com/kplindegaard/smbus2","docs":null,"changelog":null,"pypi":"https://pypi.org/project/smbus2/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null},"quickstart":{"code":"import os\nfrom smbus2 import SMBus\n\n# Example: Read a byte from an I2C device (e.g., at address 0x27, register 0x00)\n# Replace `1` with the correct I2C bus number for your system (e.g., 0 for older Raspberry Pis, 1 for newer)\n# Replace `0x27` with your device's I2C address\n# Replace `0x00` with the register to read from\n\nI2C_BUS_NUMBER = int(os.environ.get('I2C_BUS_NUMBER', '1')) # Default to bus 1\nDEVICE_ADDRESS = int(os.environ.get('I2C_DEVICE_ADDRESS', '0x27'), 16)\nREGISTER_ADDRESS = int(os.environ.get('I2C_REGISTER_ADDRESS', '0x00'), 16)\n\ntry:\n    # Using 'with' statement ensures the bus is properly closed\n    with SMBus(I2C_BUS_NUMBER) as bus:\n        byte_data = bus.read_byte_data(DEVICE_ADDRESS, REGISTER_ADDRESS)\n        print(f\"Read byte 0x{byte_data:02X} from device 0x{DEVICE_ADDRESS:02X}, register 0x{REGISTER_ADDRESS:02X} on bus {I2C_BUS_NUMBER}.\")\n\n        # Example: Write a byte to the same device/register\n        write_value = 0xAA # Example value to write\n        bus.write_byte_data(DEVICE_ADDRESS, REGISTER_ADDRESS, write_value)\n        print(f\"Written byte 0x{write_value:02X} to device 0x{DEVICE_ADDRESS:02X}, register 0x{REGISTER_ADDRESS:02X} on bus {I2C_BUS_NUMBER}.\")\n\nexcept FileNotFoundError:\n    print(f\"Error: I2C bus {I2C_BUS_NUMBER} not found. Ensure I2C is enabled and the bus number is correct.\")\nexcept Exception as e:\n    print(f\"An error occurred: {e}\")","lang":"python","description":"This quickstart demonstrates basic I2C read and write operations using the `SMBus` class with a context manager, ensuring proper resource cleanup. It also shows how to set I2C bus, device address, and register address, with placeholders for actual values and environment variables for safer execution.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":{"tag":null,"tag_description":null,"last_tested":"2026-05-15","installed_version":"0.6.1","pypi_latest":"0.6.1","is_stale":false,"summary":{"python_range":"3.10–3.9","success_rate":100,"avg_install_s":1.5,"avg_import_s":0.01,"wheel_type":"wheel"},"results":[{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"smbus2","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":null,"import_time_s":0.01,"mem_mb":0.3,"disk_size":"17.8M"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"smbus2","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":1.5,"import_time_s":0.01,"mem_mb":1.1,"disk_size":"18M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"smbus2","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":null,"import_time_s":0.01,"mem_mb":0.3,"disk_size":"19.7M"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"smbus2","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":1.5,"import_time_s":0.02,"mem_mb":1.1,"disk_size":"20M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"smbus2","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":null,"import_time_s":0.01,"mem_mb":0.3,"disk_size":"11.6M"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"smbus2","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":1.4,"import_time_s":0.02,"mem_mb":1.2,"disk_size":"12M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"smbus2","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":null,"import_time_s":0.01,"mem_mb":0.8,"disk_size":"11.3M"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"smbus2","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":1.4,"import_time_s":0.02,"mem_mb":1.3,"disk_size":"12M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"smbus2","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":null,"import_time_s":0,"mem_mb":0.3,"disk_size":"17.3M"},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"smbus2","exit_code":0,"wheel_type":"wheel","failure_reason":null,"import_side_effects":"clean","install_time_s":1.7,"import_time_s":0.02,"mem_mb":1.1,"disk_size":"18M"}]}}