Install & Compatibility
Where this runs
tested against v? · pip install
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.920 runs
build_error
glibcpy 3.10–3.920 runs
build_error
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
libvirt
✓ import libvirt
The binding module itself is named `libvirt`.
Connects to the local QEMU/KVM hypervisor, prints its hostname and libvirt version, then closes the connection. This demonstrates basic connection and resource management.
import libvirt
import sys
# Connect to the default QEMU/KVM hypervisor.
# 'qemu:///system' requires appropriate permissions (e.g., user in 'libvirt' group or root).
# For user-session hypervisor, try 'qemu:///session'.
try:
conn = libvirt.open("qemu:///system")
if conn is None:
print('Failed to open connection to qemu:///system', file=sys.stderr)
sys.exit(1)
except libvirt.libvirtError as e:
print(f'Failed to open connection: {e}', file=sys.stderr)
sys.exit(1)
print(f"Successfully connected to libvirt. Hypervisor: {conn.getHostname()}")
# Example: Get and print libvirt version
print(f"Libvirt version: {conn.getVersion()}")
# Always close the connection
conn.close()
Debug
Known issues
gotchaThe `libvirt-python` package is a binding to the `libvirt` C library. You must have the `libvirt` C library (and often its development headers) installed on your system for `libvirt-python` to work. Installing just the Python package is insufficient.fixOn Debian/Ubuntu: `sudo apt install libvirt-dev`. On RHEL/Fedora: `sudo dnf install libvirt-devel`. Ensure the `libvirtd` service is running.
affects: All versions
gotchaConnecting to hypervisors like `qemu:///system` often requires elevated privileges (e.g., running as root or having your user account in the `libvirt` UNIX group). Connection failures due to permissions are common.fixAdd your user to the `libvirt` group: `sudo usermod -a -G libvirt $USER` (then log out and back in). Alternatively, connect to `qemu:///session` for user-level virtualization or run your script with `sudo` (use with caution).
affects: All versions
deprecated`libvirt-python` versions 4.x and above (released after 2018) have dropped support for Python 2. The current version only supports Python 3.fixEnsure you are using Python 3.6 or newer. The `libvirt-python` PyPI package specifies `requires_python: >=3.6`.
affects: <=3.x for Python 2 support; >=4.0 for Python 3 only
gotchaError handling in `libvirt-python` is done via `libvirt.libvirtError` exceptions. Many operations will raise this exception on failure, requiring robust `try...except` blocks.fixWrap `libvirt` calls in `try...except libvirt.libvirtError as e:` to catch and handle API errors gracefully.
affects: All versions
Upgrade
Version history
12.4.0latest on PyPI · released Jun 1, 2026
Audit
Dependencies
No dependency data recorded yet.