Install & Compatibility
Where this runs
tested against v1.4.3 · 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.910 runs
installs and imports cleanly · install 0.0s · import 0.058s · 23.5MB
glibcpy 3.10–3.910 runs
installs and imports cleanly · install 2.4s · import 0.054s · 24MB
22MB installed
● package 22MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
SmiStarParser
✓ from pysmi.parser import SmiStarParser
SmiBuilder
✓ from pysmi.builder import SmiBuilder
PySnmpCodeGen
✓ from pysmi.codegen import PySnmpCodeGen
FileReader
✓ from pysmi.reader import FileReader
This quickstart demonstrates how to parse a simple MIB file using PySMI. It creates a temporary MIB file and a dummy 'SNMPv2-SMI' for basic dependency resolution, then uses `SmiBuilder` and `FileReader` to load and verify the MIB module.
import os
import tempfile
from pysmi.builder import SmiBuilder
from pysmi.reader import FileReader
mib_content = """
DUMMY-MIB DEFINITIONS ::= BEGIN
IMPORTS
MODULE-IDENTITY, OBJECT-TYPE, enterprises
FROM SNMPv2-SMI;
dummyMIB MODULE-IDENTITY
LAST-UPDATED "202310260000Z"
ORGANIZATION "Example"
CONTACT-INFO "example@example.com"
DESCRIPTION
"A dummy MIB for testing purposes."
REVISION "202310260000Z"
DESCRIPTION
"Initial revision."
::= { enterprises 99999 }
dummyObject OBJECT-TYPE
SYNTAX INTEGER
MAX-ACCESS read-only
STATUS current
DESCRIPTION
"A dummy object."
::= { dummyMIB 1 }
END
"""
with tempfile.TemporaryDirectory() as temp_dir:
mib_path = os.path.join(temp_dir, "DUMMY-MIB.mib")
# PySMI often needs standard MIBs; for simplicity, we mock an empty SNMPv2-SMI
# In a real scenario, you'd point to a directory with actual standard MIBs
snmpv2_smi_path = os.path.join(temp_dir, "SNMPv2-SMI.mib")
with open(snmpv2_smi_path, "w") as f:
f.write("SNMPv2-SMI DEFINITIONS ::= BEGIN END")
with open(mib_path, "w") as f:
f.write(mib_content)
mibBuilder = SmiBuilder()
mibBuilder.addMibSources(FileReader(temp_dir))
try:
mibBuilder.loadModules("DUMMY-MIB")
# Retrieve the MIB node by name or OID to verify
if mibBuilder.getMibNode(('DUMMY-MIB', 1)):
print("Successfully loaded 'DUMMY-MIB' and found 'dummyObject'.")
else:
print("Failed to find 'dummyObject' within 'DUMMY-MIB'.")
except Exception as e:
print(f"Error loading MIB: {e}")
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'pysmi'
The `pysmi-lextudio` package was not installed, or an outdated `pysmi` package (the old one) was installed, leading to incorrect import paths.
fixEnsure you have installed the correct package: `pip install pysmi-lextudio`. If you previously installed `pysmi`, uninstall it first: `pip uninstall pysmi`.
pysmi.error.SmiError: MIB module "SOME-DEPENDENT-MIB" not found
The parser could not locate a required MIB module that the MIB being processed imports from. This usually means the dependent MIB file is missing from the configured search paths.
fixAdd the directory containing `SOME-DEPENDENT-MIB.mib` (and any other dependencies) to your MIB builder's sources using `mibBuilder.addMibSources(FileReader('/path/to/mib/directory'))`. UnicodeDecodeError: 'utf-8' codec can't decode byte 0x... in position ...: invalid start byte
A MIB file is not encoded in UTF-8, but PySMI attempts to read it as such by default, leading to a decoding failure.
fixWhen adding `FileReader` sources, specify the correct encoding if known: `FileReader('/path/to/mib/directory', encoding='iso-8859-1')` (replace `iso-8859-1` with the actual encoding if different). Upgrade
Version history
1.4.3latest on PyPI · released Apr 3, 2024
Audit
Dependencies
PythonrequiredRequired runtime environment