Registry / database / python-can

python-can

JSON →
library4.6.1pypypi✓ verified 26d ago

The python-can library provides Controller Area Network (CAN) support for Python developers, offering common abstractions to various hardware devices and a suite of utilities for sending and receiving messages on a CAN bus. It supports CPython and PyPy, running on Mac, Linux, and Windows. The current version is 4.6.1, with frequent updates to the 4.x series.

pip install python-can
INSTALL
IMPORT
SIG · PYTHON-CAN
P
python-can
databasepythonv4.6.1
Install
2.4s avg
Import
364ms
Disk
20MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v4.6.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 0.386s · 21.6MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 2.4s · import 0.342s · 22MB
20MB installed
● package 20MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

can
import can
can.Bus
from can import Bus
can.Message
from can import Message

This quickstart demonstrates how to initialize a CAN bus and send a single message. It uses a `with` statement for proper resource management. For testing without physical hardware, a virtual CAN interface (like `vcan0` on Linux) can be used.

import can import os # For a runnable example without physical hardware, you can set up a virtual CAN interface: # On Linux, run in terminal: `sudo modprobe vcan && sudo ip link add dev vcan0 type vcan && sudo ip link set vcan0 up` # Then, use channel='vcan0' below. # Configure bus interface and channel (can be set via environment variables or config file too) # Using 'socketcan' with 'vcan0' is a common virtual setup on Linux. # Replace 'socketcan' and 'vcan0' with your actual interface and channel if using real hardware. BUS_INTERFACE = os.environ.get('CAN_INTERFACE', 'socketcan') CAN_CHANNEL = os.environ.get('CAN_CHANNEL', 'vcan0') try: # Using 'with' statement ensures the bus is properly shut down with can.Bus(interface=BUS_INTERFACE, channel=CAN_CHANNEL, receive_own_messages=True) as bus: # Create a CAN message message = can.Message( arbitration_id=0x123, # Standard or extended ID is_extended_id=False, # Set to True for extended IDs data=[0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88], # 0-8 bytes for CAN 2.0, up to 64 for CAN FD is_fd=False, # Set to True for CAN FD messages bitrate_switch=False, # Relevant for CAN FD error_state_indicator=False # Relevant for CAN FD ) print(f"Attempting to send message: {message}") bus.send(message) print(f"Message sent successfully on {bus.channel} using {bus.interface} interface.") # Optional: Receive a message # received_message = bus.recv(10.0) # Wait up to 10 seconds for a message # if received_message: # print(f"Received message: {received_message}") # else: # print("No message received.") except can.CanError as e: print(f"Error sending message: {e}") except Exception as e: print(f"An unexpected error occurred: {e}")
can --version
Debug
Known issues
breakingThe `socketcan_ctypes` and `socketcan_native` implementations were removed in version 4.0.0 after a deprecation period.
fix
Users migrating from versions prior to 4.0.0 should update their code to use the unified `socketcan` interface.
affects: >=4.0.0
gotchaFailing to properly shut down the CAN bus can lead to resource leaks or unexpected behavior, especially in long-running applications.
fix
Always use the `with` statement when creating a `can.Bus` instance to ensure the bus is automatically shut down upon exiting the block, e.g., `with can.Bus(...) as bus:`.
affects: <all>
gotchaHardware-specific drivers and system-level configuration are often required for `python-can` to function with physical CAN interfaces.
fix
Before using `python-can` with hardware, ensure all necessary vendor drivers are installed and system-level configurations (e.g., `sudo ip link set can0 up type can bitrate 500000` for SocketCAN) are applied. Refer to the hardware interfaces section of the official documentation.
affects: <all>
gotchaConfiguration of `python-can` can be done via code, environment variables, or a configuration file. Inconsistent or conflicting settings across these methods can lead to unexpected bus behavior.
fix
Standardize on a single configuration method where possible. If using multiple, understand the precedence rules (typically in-code configuration overrides environment variables and config files). Always explicitly set critical parameters like interface and channel.
affects: <all>
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'can'
The `python-can` library has not been installed in the current Python environment or is not accessible.
fix
pip install python-can
can.exceptions.CanInitializationError: Failed to initialize channel 'can0' for interface 'socketcan'
The specified CAN hardware interface (e.g., 'can0', 'COMx') is not available, correctly configured, or its drivers are not installed/loaded on the operating system.
fix
Ensure the CAN device is connected, its drivers are installed, and the interface is activated (e.g., `sudo ip link set can0 up type can bitrate 500000` for SocketCAN on Linux).
AttributeError: 'Bus' object has no attribute 'send_message'
The user is attempting to send a CAN message using a method name that does not exist on the `Bus` object; the correct method is `send()`.
fix
bus.send(msg)
AttributeError: 'Bus' object has no attribute 'read'
The `read()` method was deprecated and removed in `python-can` version 4.0; the recommended method for receiving a single message is `recv()`, or `iter_frames()` for iterating over messages.
fix
message = bus.recv()
Upgrade
Version history
4.6.1latest on PyPI · released Aug 12, 2025
Audit
Dependencies
Linux kernel modules (e.g., vcan, can)optionalRequired for SocketCAN interface on Linux. Needs system-level configuration using `ip` commands.
Hardware vendor drivers (e.g., Kvaser CANLib SDK, Vector XL Driver Library)optionalNecessary for interacting with specific CAN hardware on Windows or other OS.
Agent activity
18 hits · last 30 days
node
14
Resources
python-can — pip install python-can · libregistry