Hi

I posted this on another thread but messed up the title (called it y2exe) and I want to make sure this gets found if someone can help. I'm using py2exe and pysnmp, and they seem to be incompatible, at least on the oneliner form I'm using. Pysnmp claims to be
py2exe friendly, and I want to know what I'm doing wrong.

Here's the code:

from pysnmp.entity.rfc3413.oneliner import cmdgen
from pysnmp.entity import config 
from Tkinter import * 

def doCmd( key ):    
    errorIndication, errorStatus, errorIndex, varBinds = 
        cmdgen.CommandGenerator().getCmd( 
           cmdgen.CommunityData('my-agent', 'public', 0),  
           cmdgen.UdpTransportTarget(("127.0.0.1", 161 )),
           key)    
    print errorIndication
    print errorStatus    
    for item in varBinds:         
        print item 

doCmd( (1,3,6,1,4,1,557,1,1,10,3,1,1,305) )

Here's the output I get when I run this:
Produces this output:
Traceback (most recent call last):
File "", line 16, in <module>
File "", line 6, in doCmd
File "pysnmp\entity\rfc3413\oneliner\cmdgen.pyc", line 116, in __init__
File "pysnmp\entity\engine.pyc", line 16, in __init__
File "pysnmp\proto\rfc3412.pyc", line 16, in __init__
File "pysnmp\smi\builder.pyc", line 143, in __init__
File "pysnmp\smi\builder.pyc", line 35, in init
File "pysnmp\smi\builder.pyc", line 80, in _init
ImportError: No module named mibs.instances

Here's my compilation script:

from distutils.core import setupimport py2exe setup(console=['snmpbad.py'])from distutils.core import setup
import py2exe

setup(console=['snmpbad.py'])

Any help would be extremely appreciated!

Dani AI

Generated

The ImportError shown in the thread (No module named mibs.instances) is a classic py2exe/modulefinder symptom: pysnmp loads MIB modules at runtime using dynamic imports, and py2exe’s static analysis doesn’t see those names, so they are omitted from the frozen bundle. ’s suggestion to add the missing pysnmp pieces to the py2exe options is the right pattern; confirmed that including those modules fixed the runtime error, and ’s setup template demonstrates a complete packing flow.

Practical troubleshooting steps and alternatives:

  • Inspect the py2exe build log for the “missing modules” list and for what actually landed in the dist folder. That log shows which dynamic imports were skipped.
  • Add the omitted modules or entire package entries in the py2exe options (explicit inclusion or package inclusion) so modulefinder will bundle them. Using a modern call pattern like setup(**options) is clearer than apply().
  • If the library expects filesystem access to MIB resources (some dynamic loaders do), include the package as real files (data_files or unzipped packages) rather than only inside the archive; adjusting zipfile/bundle_files can be required for that.
  • For stubborn cases, copy the MIB package into the distribution manually or precompile MIBs and point pysnmp’s search path to them.

Additional packaging pitfalls to watch for:

  • GUI apps that import Tkinter need tcl/tk DLLs and runtime files copied into dist; verify those DLLs appear and the tcl library paths are correct.
  • C-extension or runtime-only imports can be missed; confirm the dist works on a clean machine (no Python installed) to validate completeness.

Summary: explicit inclusion of pysnmp’s dynamically loaded MIB modules (or packaging them as real files) is the robust fix. If py2exe proves difficult to configure for this pattern, consider alternative packagers (cx_Freeze, PyInstaller) which sometimes handle runtime-imported resources with less manual configuration.

Recommended Answers

All 7 Replies

Dident get you pysnmp script to work.
Here is a working py2exe kode.

from distutils.core import setup
import py2exe
import sys

def py_exe(file_in=None):
    if len(sys.argv) == 1:
        sys.argv.append('py2exe')

    setup(options = {'py2exe': {'compressed': 1, 'optimize': 2, 'ascii': 1, 'bundle_files': 3}},
           zipfile = None,

           ## Can use console or window
           ## Filpath to '....py' or same folder as you run this py file from
           console = [{'script': 'your_script.py'}])
py_exe()

You can also try Gui2exe.
http://code.google.com/p/gui2exe/

When I run it, I have to type

setup py2exe

Perhaps that would execute it. I'll try yours, it looks much more complete. I'll also check into gui2exe.

When I run it, I have to type

setup py2exe

You run it as every other python code.
From your editor or command line python name_of_script.py

Make sure you are running the latest pysnmp and have the following code in your app's setup.py:

...
if "py2exe" in sys.argv:
    import py2exe
    # fix executables
    options['console'] = options['scripts']
    del options['scripts']
    # add files not found by modulefinder
    options['options'] = {
        'py2exe': {
            'includes': [
                'pysnmp.smi.mibs.*',
                'pysnmp.smi.mibs.instances.*'
                ]
            }
        }

apply(setup, (), options)

AH! The 'includes' was the piece of the puzzle I was missing. I was not able to understand how to incorporate your fragments into my setup.py. Here is how I modified my to use the options argument:

from distutils.core import setup
import py2exe

setup( console = [
               { "script": "monitor.py", 
                  "icon_resources": [(0, "monitor.ico")]
               }],
       options = { 
            "py2exe":{
                'includes': [                
                    'pysnmp.smi.mibs.*',                
                    'pysnmp.smi.mibs.instances.*'                
                    ]            
            }        
        }
)

This worked for me, and now when I run the snmp command that was failing, it works as expected. THANK YOU! THANK YOU!

So I can better understand, could you repost you setup code as a complete program? I have never used the apply() function; it's deprecated, but from what I read I can see the usefulness of it. I could not figure out how to set up the rest of the file such that the apply() did what I wanted, with the rest of the code. I've been writing Python for about 7 months now, and I'm getting pretty comfortable with what I do, but there are so many ways to do things in this language I feel I'll be learning them forever.

Thank you for helping and posting to this thread.

That was a code snippet from the pysnmp-apps package. You could
download and check it out.

However you do not really need to use the apply() function. In the
pysnmp-apps case, it was used to invoke setup() function and pass it
a [conditionally] pre-filled dictionary of options.

There are other, more modern, ways to do that (e.g. setup(**options) ).

Popper, thanks again for solving my problem. I really appreciate the help and information.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.