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
muslpy 3.10–3.910 runs
installs and imports cleanly · install 0.0s · import 0.386s · 21.6MB
glibcpy 3.10–3.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.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
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.
fixpip 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.
fixEnsure 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()`.
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.
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.