pysnmp cannot import asn1
Today I tried to use an old Python script to do something with SNMP. The script was importing some SNMP library using the following code:
(this is basically from the snmpget example on the pysnmp website).
Installing python-pysnmp didn't work, there's a few versions of the pysnmp API available and apparently the code above is assuming version 2 of the API, so we need to install python-pysnmp2
ramdyne@host:~$ sudo apt-get install python-pysnmp2 xxx ramdyne@host:~$ ./script.py Traceback (most recent call last): File "./script.py", line 7, in <module> from pysnmp import asn1, v2c, role ImportError: cannot import name asn1
Obviously that didn't work, but why? After a lot of investigating, it looks like the current Debian python-pysnmp2 package includes both versions 2 and 4 of the API and you need to explicitly choose which one you want to use before importing pysnmp in your python code.
Choosing the API version is done using an environment variable. You can do this in your commandline shell (like bash), but I prefer to do this explicitly in the script itself:
(choosing version 4 of the API is left as an exercise for the reader.)