Registry / serialization / pysmi
library2.0.0pypypi✓ verified 25d ago

PySMI is a pure-Python implementation of an SNMP SMI MIB parser and conversion library. It takes ASN.1 MIBs and can transform them into various formats, including JSON documents and PySNMP-compatible Python modules. It understands SMIv1, SMIv2, and common de-facto SMI dialects, and can automatically pull MIBs from local directories, ZIP archives, and HTTP servers. The current version is 1.6.3 and it actively supports Python 3.9+.

pip install pysmi
INSTALL
IMPORT
SIG · PYSMI
P
pysmi
serializationpythonv2.0.0
Install
2.5s avg
Import
502ms
Disk
22MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v2.0.0 · 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.95 runs
installs and imports cleanly · install 0.0s · import 0.524s · 23.9MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 2.5s · import 0.480s · 24MB
22MB installed
● package 22MB
Code
Verified usage

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

MibCompiler
from pysmi.compiler import MibCompiler
SmiV2Parser
from pysmi.parser.smi import SmiV2Parser
JsonCodeGen
from pysmi.codegen.json import JsonCodeGen
PySnmpCodeGen
from pysmi.codegen.pysnmp import PySnmpCodeGen
FileReader
from pysmi.reader.localfile import FileReader
CallbackWriter
from pysmi.writer.callback import CallbackWriter

This quickstart demonstrates how to compile an in-memory ASN.1 MIB module (`FOO-MIB`) into a JSON document using PySMI's `MibCompiler`. It sets up a `CallbackReader` to supply the MIB content and a `CallbackWriter` to print the resulting JSON to the console, illustrating the core transformation process.

import os from pysmi.compiler import MibCompiler from pysmi.parser.smi import SmiV2Parser from pysmi.codegen.json import JsonCodeGen from pysmi.reader.callback import CallbackReader from pysmi.writer.callback import CallbackWriter # Example MIB content (simplified for demonstration) mib_content = """ FOO-MIB DEFINITIONS ::= BEGIN IMPORTS MODULE-IDENTITY, OBJECT-TYPE, enterprises FROM SNMPv2-SMI; fooModule MODULE-IDENTITY LAST-UPDATED "202301010000Z" ORGANIZATION "Example Corp" CONTACT-INFO "support@example.com" DESCRIPTION "The MIB module for example devices." REVISION "202301010000Z" DESCRIPTION "Initial revision." ::= { enterprises 99999 } fooObjects OBJECT IDENTIFIER ::= { fooModule 1 } fooStatus OBJECT-TYPE SYNTAX INTEGER { up(1), down(2) } MAX-ACCESS read-only STATUS current DESCRIPTION "The operational status of a foo device." ::= { fooObjects 1 } END """ def store_json_mib(mib_name, mib_data, *args): print(f"--- Compiled MIB: {mib_name} ---") print(mib_data) # Initialize compiler infrastructure mibCompiler = MibCompiler( SmiV2Parser(), JsonCodeGen(), CallbackWriter(store_json_mib) ) # Add a reader that can provide our MIB content by name mibCompiler.addSources(CallbackReader(lambda mibName, **kwargs: mib_content if mibName == 'FOO-MIB' else None)) # Compile the MIB # Note: 'rebuild=True' forces compilation even if cached, 'genTexts=True' includes MIB descriptions results = mibCompiler.compile('FOO-MIB', rebuild=True, genTexts=True) print(f"Compilation results: {results}")
mibdump --version
Debug
Known issues
gotchaBy default, PySMI does not include MIB descriptions, comments, or references in the generated output (e.g., JSON or PySNMP modules) to minimize size. This can lead to a less verbose output than expected.
fix
Pass `genTexts=True` to the `MibCompiler.compile()` method to include full textual descriptions and references from the MIB.
affects: All versions
gotchaPySMI performs caching and will not recompile a MIB if a 'fresh enough' version already exists in its search paths. This can be unexpected during development or when changes are made to source MIBs.
fix
To force unconditional recompilation, pass `rebuild=True` to the `MibCompiler.compile()` method. For parser lookup tables, configure a cache directory using `--cache-directory` option (if using `mibdump.py`) or similar API for performance.
affects: All versions
gotchaSome foundational MIBs (e.g., SNMPv2-SMI, SNMPv2-TC, RFC-1212) contain base SMI types or ASN.1 MACRO definitions that should generally not be transformed by PySMI. Attempting to compile these can lead to errors or incorrect output.
fix
Explicitly 'blacklist' such MIBs using `StubSearcher` in your `MibCompiler` configuration. `PySnmpCodeGen.baseMibs` provides a pre-defined list for PySNMP target generation. For example: `mibCompiler.addSearchers(StubSearcher(*PySnmpCodeGen.baseMibs))`.
affects: All versions
gotchaPoorly formed or 'broken' ASN.1 MIBs are common in practice and can cause parsing failures. While PySMI attempts workarounds, severely broken MIBs may not compile.
fix
Utilize PySMI's 'borrow' feature by configuring a source for pre-compiled MIBs. This allows PySMI to fetch already transformed MIBs if the source ASN.1 MIB cannot be found or parsed. Example: `mibCompiler.addSearchers(PyFileSearcher('/path/to/precompiled/mibs'))` or an `HttpReader` to `mibs.snmplabs.com`.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'pysmi.reader.localfile'
The module 'pysmi.reader.localfile' does not exist; the correct module is 'pysmi.reader.localfile.FileReader'.
fix
from pysmi.reader.localfile import FileReader
pysmi: failing on error Unknown parent symbol: mib_2 at MIB HOST-RESOURCES-MIB
The MIB file references a parent symbol 'mib_2' that is not defined or imported.
fix
Ensure that the MIB file includes the necessary imports for all referenced symbols, such as 'mib-2'.
pysmi: failing on error Bad grammar near token type LOWERCASE_IDENTIFIER, value lldpinfo at MIB HH3C-SERVER-AGENT-MIB
The MIB file contains a syntax error, possibly due to an invalid identifier or incorrect grammar.
fix
Review the MIB file for syntax errors, ensuring all identifiers and grammar conform to SMI standards.
pysnmp.smi.error.MibNotFoundError: 03409602D__Alpha_System_Controller compilation error(s): missing
The specified MIB file '03409602D__Alpha_System_Controller' is missing or cannot be found.
fix
Verify that the MIB file exists at the specified location and that the path is correct.
smidump: module 'custom-mib' contains errors, expect flawed output
The MIB file 'custom-mib' contains syntax or semantic errors that prevent successful parsing.
fix
Use a MIB compiler like 'pysmi' to parse and validate the MIB file, correcting any reported errors.
Upgrade
Version history
2.0.0latest on PyPI · released Apr 26, 2026
Audit
Dependencies
pysnmpoptionalRequired if generating PySNMP-compatible Python modules for use with pysnmp, but not a direct runtime dependency for core parsing/JSON generation.
Agent activity
16 hits · last 30 days
node
12
Resources
pysmi — pip install pysmi · libregistry