Registry / devops / pyang
library2.7.1pypypi✓ verified 87d ago

A YANG (RFC 6020/7950) validator and converter, `pyang` provides tools for parsing, validating, and transforming YANG modules. It is primarily used as a command-line tool but also offers a Python API for programmatic access to perform validation, convert modules to formats like YIN (XML) or tree structures, and generate code. The project maintains an `active` development pace with frequent minor releases, currently at version 2.7.1.

pip install pyang
INSTALL
IMPORT
SIG · PYANG
P
pyang
devopspythonv2.7.1
Install
2.2s avg
Import
175ms
Disk
30MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v2.7.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.920 runs
installs and imports cleanly · install 0.0s · import 0.182s · 31.5MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 2.2s · import 0.168s · 32MB
30MB installed
● package 30MB
Code
Verified usage

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

plugin
from pyang import plugin
Required for initializing pyang's plugin system which provides various functionalities like output formats.
repository
from pyang import repository
Provides classes like FileRepository to manage YANG module lookup paths.
context
from pyang import context
The central object (Context) for a pyang parsing and validation session.
error
from pyang import error
Provides utilities for handling and formatting pyang's validation errors and warnings.

This quickstart demonstrates how to programmatically load and validate a YANG module using `pyang`'s Python API. It creates a dummy YANG file, initializes the plugin system, sets up a file repository, and then adds and validates the module within a context.

import os import tempfile from pyang import plugin, repository, context, error # Create a dummy YANG file content yang_content = """ module example-module { namespace "urn:example:module"; prefix "ex"; container my-container { leaf my-leaf { type string; description "A simple leaf."; } } } """ # Create a temporary directory and file for the YANG module temp_dir = tempfile.mkdtemp() yang_file_path = os.path.join(temp_dir, 'example-module.yang') with open(yang_file_path, 'w') as f: f.write(yang_content) try: # 1. Initialize pyang plugins # This is crucial as many functionalities (like output formats) are plugins plugin.init() # 2. Create a repository pointing to the directory containing our YANG module repo = repository.FileRepository(temp_dir) # 3. Create a pyang context (a parsing and validation session) ctx = context.Context(repo) ctx.opts.add_opts = [] # Ensure no unexpected options interfere # 4. Load the module module = ctx.add_module(yang_file_path) if module: print(f"Successfully loaded module: {module.arg}") # 5. Validate the context for errors and warnings ctx.validate() if ctx.errors: print("Validation issues found:") for e in ctx.errors: print(f" {error.err_to_str(e)}") else: print("Module validated successfully with no errors or warnings.") else: print(f"Failed to load module from {yang_file_path}") except Exception as e: print(f"An unexpected error occurred: {e}") finally: # Clean up the temporary file and directory os.remove(yang_file_path) os.rmdir(temp_dir)
pyang --version
Debug
Known issues
gotchaUsers often expect `pyang` to output various formats (tree, JSON, YIN, etc.) directly after loading a module via the API. However, these functionalities are provided by plugins that must be initialized and potentially explicitly called or activated within the `Context` to produce the desired output, mimicking command-line flags. Without proper plugin setup, `ctx.emit()` might not produce expected results.
fix
Initialize plugins with `plugin.init()` and ensure relevant output plugins are loaded or explicitly used with the `Context` to leverage their functionality. For CLI-like output, you might need to replicate the `pyang` script's logic for invoking plugins.
affects: All versions when using the Python API for output.
gotchaWhen using `repository.FileRepository`, ensuring that the repository paths correctly include directories containing both the primary YANG module and any modules it `import`s is crucial. `pyang` will fail to resolve dependencies if they are not found within the configured repository paths, leading to "module not found" errors during `add_module` or `validate`.
fix
Pass the correct directory paths to `FileRepository`'s constructor, or use `ctx.repository.add_path()` for additional lookup directories. Ensure all imported YANG modules are accessible via these configured paths.
affects: All versions.
breakingIn version 2.3.2, `pyang` reverted a fix for issue #587, causing it to switch back to using the XML Schema regular expression engine for pattern validation. This means that YANG modules relying on specific behaviors or features of the previously used Python `re` engine for pattern matching might exhibit different validation results (e.g., a module that was previously valid might become invalid, or vice-versa) when moving to or from versions where this change was active.
fix
Thoroughly re-validate YANG modules, especially those with complex `pattern` restrictions, when upgrading to or from `pyang` 2.3.2 and later. Adjust patterns if necessary to comply with the XML Schema regex engine's semantics.
affects: Upgrading to or from `pyang` 2.3.2 and later, particularly from versions where a non-XML Schema regex engine was in use.
gotchaOlder versions of `pyang` (prior to approximately 2.4.0) could crash when installed with `pip` versions 10.0.0 or higher due to changes in `pip`'s internal structure (specifically, the removal of `pip.locations`). Users attempting to install older `pyang` or run `pyang` in environments with `pip` 10+ might encounter `AttributeError: module 'pip' has no attribute 'locations'`.
fix
Upgrade `pyang` to version 2.4.0 or newer. If an upgrade is not possible, use `pip < 10.0.0` or run `pyang` within a virtual environment.
affects: Versions of `pyang` prior to ~2.4.0 when used with `pip` 10.0.0 or later.
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'pyang'
The 'pyang' Python package is not installed in the current Python environment or is not accessible in the Python path.
fix
Install pyang using pip: `pip install pyang`
'pyang' is not recognized as an internal or external command, operable program or batch file.
The directory containing the 'pyang' executable script (typically in your Python environment's 'Scripts' folder on Windows) is not included in your system's PATH environment variable.
fix
Either add the Python 'Scripts' directory to your system's PATH, or invoke pyang directly via Python: `python -m pyang <arguments>`
error: module "<module-name>" not found in search path
pyang cannot locate a YANG module (or submodule) that is imported or included by the main YANG module you are processing, because its directory is not specified in pyang's search path.
fix
Specify the directory containing the dependent YANG modules using the `-p` or `--path` option, for example: `pyang -p /path/to/yang/modules your-module.yang`
ModuleNotFoundError: No module named 'pkg_resources'
This error occurs in Python 3.12+ environments where the 'pkg_resources' module has been removed from the standard library. Older pyang versions might implicitly depend on it without explicitly listing 'setuptools' as a dependency.
fix
Ensure 'setuptools' is installed in your environment: `pip install setuptools`. Alternatively, upgrade pyang to version 2.6.1 or newer which addresses this compatibility issue.
Upgrade
Version history
2.7.1latest on PyPI · released Aug 29, 2025
Audit
Dependencies

No dependency data recorded yet.

Agent activity
12 hits · last 30 days
node
10
Amazon
1
Resources