Install & Compatibility
Where this runs
tested against v7.1.29 · 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.95 runs
installs and imports cleanly · install 0.0s · import 0.000s · 22.1MB
glibcpy 3.10–3.95 runs
installs and imports cleanly · install 1.9s · import 0.000s · 23MB
20MB installed
● package 20MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
*
✓ from pysnmp.hlapi import *
The 'hlapi' (High-Level API) is the recommended way to interact with pysnmp for most common tasks (e.g., GET, SET, WALK). It abstracts away much of the low-level PDU construction.
SnmpEngine
✓ from pysnmp.hlapi import SnmpEngine
CommunityData
✓ from pysnmp.hlapi import CommunityData
UdpTransportTarget
✓ from pysnmp.hlapi import UdpTransportTarget
ContextData
✓ from pysnmp.hlapi import ContextData
ObjectType
✓ from pysnmp.hlapi import ObjectType
ObjectIdentity
✓ from pysnmp.hlapi import ObjectIdentity
cmdgen
✓ from pysnmp.hlapi import getCmd
✗ from pysnmp.entity.rfc3413.oneliner import cmdgen
While 'cmdgen' from 'oneliner' used to be common in older versions (e.g., pysnmp4), the 'hlapi' functions like 'getCmd', 'nextCmd', 'setCmd' are the preferred and more robust way in pysnmp 5.x+.
This quickstart demonstrates how to perform a basic SNMP GET request using the high-level API (`pysnmp.hlapi`). It fetches a single OID (sysDescr.0 by default) from a specified host using a community string (SNMPv2c). The example handles potential error conditions returned by the SNMP agent.
import os
from pysnmp.hlapi import (
SnmpEngine, CommunityData, UdpTransportTarget,
ContextData, ObjectType, ObjectIdentity, getCmd
)
def snmp_get(host, community, oid):
errorIndication, errorStatus, errorIndex, varBinds = next(
getCmd(
SnmpEngine(),
CommunityData(community, mpModel=0), # mpModel=0 for SNMPv1, 1 for SNMPv2c
UdpTransportTarget((host, 161)),
ContextData(),
ObjectType(ObjectIdentity(oid))
)
)
if errorIndication:
return f"Error: {errorIndication}"
elif errorStatus:
return f"Error: {errorStatus.prettyPrint()} at {varBinds[int(errorIndex)-1] if errorIndex else '?'}"
else:
return ', '.join([f'{x[0].prettyPrint()} = {x[1].prettyPrint()}' for x in varBinds])
# Example Usage:
# You can use a public SNMP demo server for testing, e.g., 'demo.snmplabs.com'
# Make sure to replace these with actual values for production environments
SNMP_HOST = os.environ.get('SNMP_HOST', 'demo.snmplabs.com')
SNMP_COMMUNITY = os.environ.get('SNMP_COMMUNITY', 'public')
# OID for sysDescr.0 (system description)
SNMP_OID = os.environ.get('SNMP_OID', '1.3.6.1.2.1.1.1.0')
if __name__ == '__main__':
print(f"Attempting SNMP GET on {SNMP_HOST} for OID {SNMP_OID} with community '{SNMP_COMMUNITY}'")
result = snmp_get(SNMP_HOST, SNMP_COMMUNITY, SNMP_OID)
print(f"SNMP GET Result: {result}")
Debug
Known issues
breakingMajor API changes when migrating from `pysnmp4` to `pysnmp` (version 5.x and later). `pysnmp4` is a completely separate package and its API is incompatible with the modern `pysnmp` library.fixRewrite existing code to use the `pysnmp.hlapi` or the updated low-level API constructs. Refer to the official `pysnmp` documentation for migration guides and new API examples.
affects: All versions when migrating from 'pysnmp4' to 'pysnmp >= 5.0'
gotchaMIB (Management Information Base) loading and management can be complex, especially for custom or non-standard MIB files. Users often struggle with configuring `mibBuilder` correctly.fixFor standard OIDs, `hlapi` often handles MIB resolution automatically. For custom MIBs, explicitly use `mibBuilder.loadModules()` and `mibBuilder.addMibSources()` to point to the correct MIB files and directories. Ensure MIB files are accessible and correctly formatted.
affects: All versions
gotchapysnmp offers both synchronous and asynchronous (Twisted, asyncio) APIs. Mixing these or misunderstanding which API flavor is being used can lead to blocking issues in event-driven applications or unexpected behavior.fixFor simple scripts, the synchronous `hlapi` (as in the quickstart) is sufficient. For event-driven applications, explicitly use `pysnmp.hlapi.asyncio` or `pysnmp.hlapi.twisted` with their respective `SnmpEngine` implementations and ensure your application's event loop is properly integrated.
affects: All versions
gotchaThe packaging structure for `pysnmp` (version 7.x) includes a dependency on `pysnmp-lextudio`, which can cause confusion. While `pysnmp-lextudio` is a required dependency, it often appears as a minimal, internal package.fixAlways install `pysnmp` directly (`pip install pysnmp`). The package manager will automatically pull in `pysnmp-lextudio`. Avoid installing `pysnmp-lextudio` separately unless explicitly required for debugging or advanced scenarios, as it's primarily an internal component.
affects: pysnmp >= 7.0
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'pysnmp.hlapi'; 'pysnmp' is not a package
This error occurs when the 'pysnmp' module is not installed or is installed incorrectly.
fixEnsure that 'pysnmp' is installed by running 'pip install pysnmp'. If the issue persists, check for any naming conflicts with your script or directory names.
ModuleNotFoundError: No module named 'pysnmp.smi'; 'pysnmp' is not a package
This error can occur if there's a naming conflict, such as naming your script 'pysnmp.py', which interferes with the module import.
fixRename your script to avoid conflicts, for example, to 'test_pysnmp.py'.
ImportError: No module named 'pyasn1.compat.octets'
This error indicates that the 'pyasn1' module is either not installed or is outdated.
fixInstall or upgrade 'pyasn1' by running 'pip install --upgrade pyasn1'.
ModuleNotFoundError: No module named 'pysnmp'
This error occurs when the 'pysnmp' module is not installed in your Python environment.
fixInstall 'pysnmp' using 'pip install pysnmp'.
ImportError: No module named 'hlapi'
This error suggests that the 'pysnmp' module is not installed or is not installed correctly.
fixEnsure 'pysnmp' is installed by running 'pip install pysnmp'.
Upgrade
Version history
7.1.29latest on PyPI · released Aug 21, 2026
Audit
Dependencies
pysnmp-lextudiorequiredRequired by pysnmp for core functionality as part of its packaging structure.
pycryptodomeoptionalOptional dependency for cryptographic operations, primarily used with SNMPv3 security features.