<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="../assets/xml/rss.xsl" media="all"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>ramdyne (Posts about pysnmp)</title><link>https://www.ramdyne.nl/</link><description></description><atom:link href="https://www.ramdyne.nl/categories/pysnmp.xml" rel="self" type="application/rss+xml"></atom:link><language>en</language><lastBuildDate>Sat, 07 Mar 2026 00:09:01 GMT</lastBuildDate><generator>Nikola (getnikola.com)</generator><docs>http://blogs.law.harvard.edu/tech/rss</docs><item><title>pysnmp cannot import asn1</title><link>https://www.ramdyne.nl/posts/pysnmp-cannot-import-asn1/</link><dc:creator>ramdyne</dc:creator><description>&lt;p&gt;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:&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre class="code python"&gt;&lt;a id="rest_code_4d9a5015c3f542109a412c3df16903e6-1" name="rest_code_4d9a5015c3f542109a412c3df16903e6-1" href="https://www.ramdyne.nl/posts/pysnmp-cannot-import-asn1/#rest_code_4d9a5015c3f542109a412c3df16903e6-1"&gt;&lt;/a&gt;&lt;span class="kn"&gt;from&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;pysnmp&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;asn1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;v2c&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;role&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;(this is basically from the &lt;a class="reference external" href="http://pysnmp.sourceforge.net/examples/2.x/snmpget.html"&gt;snmpget example&lt;/a&gt; on the pysnmp website).&lt;/p&gt;
&lt;p&gt;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&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre class="code text"&gt;&lt;a id="rest_code_2d336fa4a87a4a408170570c1d93d41e-1" name="rest_code_2d336fa4a87a4a408170570c1d93d41e-1" href="https://www.ramdyne.nl/posts/pysnmp-cannot-import-asn1/#rest_code_2d336fa4a87a4a408170570c1d93d41e-1"&gt;&lt;/a&gt;ramdyne@host:~$ sudo apt-get install python-pysnmp2
&lt;a id="rest_code_2d336fa4a87a4a408170570c1d93d41e-2" name="rest_code_2d336fa4a87a4a408170570c1d93d41e-2" href="https://www.ramdyne.nl/posts/pysnmp-cannot-import-asn1/#rest_code_2d336fa4a87a4a408170570c1d93d41e-2"&gt;&lt;/a&gt;xxx
&lt;a id="rest_code_2d336fa4a87a4a408170570c1d93d41e-3" name="rest_code_2d336fa4a87a4a408170570c1d93d41e-3" href="https://www.ramdyne.nl/posts/pysnmp-cannot-import-asn1/#rest_code_2d336fa4a87a4a408170570c1d93d41e-3"&gt;&lt;/a&gt;ramdyne@host:~$ ./script.py
&lt;a id="rest_code_2d336fa4a87a4a408170570c1d93d41e-4" name="rest_code_2d336fa4a87a4a408170570c1d93d41e-4" href="https://www.ramdyne.nl/posts/pysnmp-cannot-import-asn1/#rest_code_2d336fa4a87a4a408170570c1d93d41e-4"&gt;&lt;/a&gt;Traceback (most recent call last):
&lt;a id="rest_code_2d336fa4a87a4a408170570c1d93d41e-5" name="rest_code_2d336fa4a87a4a408170570c1d93d41e-5" href="https://www.ramdyne.nl/posts/pysnmp-cannot-import-asn1/#rest_code_2d336fa4a87a4a408170570c1d93d41e-5"&gt;&lt;/a&gt;    File "./script.py", line 7, in &amp;lt;module&amp;gt;
&lt;a id="rest_code_2d336fa4a87a4a408170570c1d93d41e-6" name="rest_code_2d336fa4a87a4a408170570c1d93d41e-6" href="https://www.ramdyne.nl/posts/pysnmp-cannot-import-asn1/#rest_code_2d336fa4a87a4a408170570c1d93d41e-6"&gt;&lt;/a&gt;        from pysnmp import asn1, v2c, role
&lt;a id="rest_code_2d336fa4a87a4a408170570c1d93d41e-7" name="rest_code_2d336fa4a87a4a408170570c1d93d41e-7" href="https://www.ramdyne.nl/posts/pysnmp-cannot-import-asn1/#rest_code_2d336fa4a87a4a408170570c1d93d41e-7"&gt;&lt;/a&gt;ImportError: cannot import name asn1
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Obviously that didn't work, but why? After a lot of investigating, it looks
like the current Debian python-pysnmp2 package includes &lt;em&gt;both&lt;/em&gt; 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.&lt;/p&gt;
&lt;p&gt;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:&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre class="code python"&gt;&lt;a id="rest_code_48505222418a4892bdc83c7289b37509-1" name="rest_code_48505222418a4892bdc83c7289b37509-1" href="https://www.ramdyne.nl/posts/pysnmp-cannot-import-asn1/#rest_code_48505222418a4892bdc83c7289b37509-1"&gt;&lt;/a&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'PYSNMP_API_VERSION'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'v2'&lt;/span&gt;
&lt;a id="rest_code_48505222418a4892bdc83c7289b37509-2" name="rest_code_48505222418a4892bdc83c7289b37509-2" href="https://www.ramdyne.nl/posts/pysnmp-cannot-import-asn1/#rest_code_48505222418a4892bdc83c7289b37509-2"&gt;&lt;/a&gt;&lt;span class="kn"&gt;from&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;pysnmp&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;asn1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;v2c&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;role&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;(choosing version 4 of the API is left as an exercise for the reader.)&lt;/p&gt;</description><category>asni</category><category>pysnmp</category><category>pysnmp2</category><category>python</category><category>role</category><category>v2c</category><guid>https://www.ramdyne.nl/posts/pysnmp-cannot-import-asn1/</guid><pubDate>Thu, 07 Nov 2013 13:47:14 GMT</pubDate></item></channel></rss>