Registry / devops / pyangbind

pyangbind

JSON →
library0.8.7pypypi✓ verified 87d ago

PyangBind is a plugin for `pyang` that converts YANG data models into a Python class hierarchy, enabling Python to manipulate data conforming to a YANG model. It facilitates programmatic interaction with network device configurations and operational state. The current version is 0.8.7, and releases are typically made a few times per year, often driven by bug fixes or new feature support.

pip install pyangbind
INSTALL
IMPORT
SIG · PYANGBIND
P
pyangbind
devopspythonv0.8.7
Install
3.0s avg
Import
Disk
33MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
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
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.000s · 34.7MB
glibc
py 3.103.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')
Debug
Known issues
breakingPyangBind 0.8.3 removed the `bitarray` dependency for the `binary` YANG type, replacing it with `pyangbind.lib.yangtypes.YANGBinary`. If you relied on `bitarray`'s API for binary data, your code will break.
fix
Update any code interacting with YANG `binary` types to use the `YANGBinary` object directly or convert it as needed. If you have existing bindings, regenerate them with PyangBind 0.8.3+.
affects: >=0.8.3
breakingAfter PyangBind 0.8.0, Python 2 support was gradually phased out, and current versions (0.8.1+) officially require Python 3.7+.
fix
Ensure your environment uses Python 3.7 or newer. Upgrade your Python installation if necessary.
affects: >=0.8.1
gotchaMany bug fixes and feature additions (e.g., related to unique leaf-lists in 0.8.1, or `bits` type handling in 0.8.4/0.8.7) require regenerating your Python bindings for the changes to take effect. Simply upgrading the `pyangbind` library is not enough.
fix
Always regenerate your Python bindings (`pyang -f pybind -o ...`) after upgrading `pyangbind` or when applying a fix that impacts generated code.
affects: All versions
gotchaYANG identifiers (module names, container names, leaf names) that clash with Python reserved keywords (e.g., `class`, `import`, `async`) can cause `SyntaxError` when generated. While `pyangbind` attempts to handle some cases by appending underscores (e.g., `async_`), it's not foolproof.
fix
Avoid using Python reserved keywords as YANG identifiers where possible. If unavoidable, manually inspect generated Python code for `SyntaxError` and adjust either the YANG model or the generated code (if feasible and maintainable).
affects: All versions
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.
fix
Ensure `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.
fix
Verify 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.
fix
Refactor 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.
fix
Rename 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.
Agent activity
7 hits · last 30 days
node
6
Resources
pyangbind — pip install pyangbind · libregistry