Install & Compatibility
Where this runs
tested against v0.8.7 · 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.920 runs
installs and imports cleanly · install 0.0s · import 0.000s · 34.7MB
glibcpy 3.10–3.920 runs
installs and imports cleanly · install 3.0s · import 0.000s · 36MB
33MB installed
● package 33MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
PybindBase
✓ from pyangbind.lib.base import PybindBase
Base class for generated YANG objects.
pybind_to_dict
✓ from pyangbind.lib.serialise import pybind_to_dict
Utility to convert a PyangBind object to a Python dictionary (e.g., for JSON/YAML serialization).
RestrictedPrecisionDecimal
✓ from pyangbind.lib.yangtypes import RestrictedPrecisionDecimal
Type used for YANG 'decimal64' types to ensure precision.
This quickstart demonstrates how to define a simple YANG model, generate Python bindings using the `pyang` tool with the `pybind` plugin, and then interact with the generated Python classes to set data and serialize it to a dictionary.
import os
# 1. Create a simple YANG model file (e.g., 'example.yang')
yang_model_content = '''
module example {
yang-version 1;
namespace "urn:example:pyangbind:test";
prefix "ex";
container config {
leaf hostname {
type string;
description "Device hostname.";
}
leaf-list interfaces {
type string;
description "List of interfaces.";
}
}
}
'''
with open('example.yang', 'w') as f:
f.write(yang_model_content)
# 2. Generate Python bindings using pyang
# This command is typically run from the command line, not Python.
# For demonstration, we simulate it.
# In a real scenario, you'd run: pyang -f pybind -o my_bindings.py example.yang
import subprocess
try:
subprocess.run(['pyang', '-f', 'pybind', '-o', 'my_bindings.py', 'example.yang'], check=True)
print("Bindings generated successfully to my_bindings.py")
except FileNotFoundError:
print("Error: 'pyang' command not found. Ensure pyangbind is installed correctly.")
exit(1)
except subprocess.CalledProcessError as e:
print(f"Error generating bindings: {e}")
print(e.stderr.decode())
exit(1)
# 3. Use the generated Python classes
import my_bindings
from pyangbind.lib.serialise import pybind_to_dict
# Instantiate the top-level module class
# The class name is typically derived from the YANG module name (e.g., 'example')
inst = my_bindings.example()
# Set values for the 'config' container and its leaves
inst.config.hostname = "my-device-1"
inst.config.interfaces.add("eth0")
inst.config.interfaces.add("eth1")
# Access and print values
print(f"Hostname: {inst.config.hostname}")
print(f"Interfaces: {list(inst.config.interfaces)}")
# Serialize the object to a dictionary
data_dict = pybind_to_dict(inst)
print("\nSerialized data:")
import json
print(json.dumps(data_dict, indent=2))
# Clean up generated files
os.remove('example.yang')
os.remove('my_bindings.py')
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'pyang'
The `pyang` dependency, which is essential for running the `pyang` command and the `pybind` plugin, is missing from your environment.
fixEnsure `pyangbind` is installed correctly with its dependencies. Run `pip install pyangbind`.
AttributeError: 'Config' object has no attribute 'interface'
You are trying to access a YANG element (container, leaf, list) that does not exist at the specified path in your generated Python object, or there's a typo in the attribute name.
fixVerify the exact path and name of the YANG element in your YANG model and ensure it matches how you're accessing it in Python (e.g., `my_module.config.interface` vs `my_module.config.interfaces`).
TypeError: 'YANGBinary' object cannot be interpreted as a buffer
Your code is trying to use a `YANGBinary` object (introduced in PyangBind 0.8.3+) as if it were the deprecated `bitarray` object or a raw byte string, which is incompatible.
fixRefactor your code to correctly handle `pyangbind.lib.yangtypes.YANGBinary` objects. Use its methods or convert it to `bytes` if needed, rather than directly treating it as a `bitarray`.
SyntaxError: invalid syntax (in generated_bindings.py)
A YANG identifier in your model (e.g., a leaf or container name) clashes with a Python reserved keyword (like `class`, `async`, `import`), leading to invalid Python syntax in the generated file.
fixRename the clashing identifier in your YANG model or implement a custom workaround in the generation process if the YANG model cannot be changed.
Upgrade
Version history
0.8.7latest on PyPI · released Dec 10, 2025
Audit
Dependencies
pyangrequiredCore dependency for YANG parsing and plugin execution.
lxmlrequiredUsed for XML serialization and deserialization.
setuptoolsrequiredBuild-time dependency for packaging.