Can't get pysnmp to work with pyinstaller

2020-05-09 03:43发布

Attempting to get pyinstaller to work with pysnmp Here is the spec file

# -*- mode: python -*-
a = Analysis(['app.py'],
             pathex=['/home/robertja/pysnmp'],
             hiddenimports=None,
             hookspath=None,
             runtime_hooks=None,
)
x = Tree('</long prefix>/pysnmp/smi/mibs',prefix='pysnmp_mibs',excludes=None)
pyz = PYZ(a.pure)
exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          x,
          name='app',
          debug=False,
          strip=None,
          upx=True,
          console=True )

Here is the program file

#!/usr/bin/python
from pysnmp.entity.rfc3413.oneliner import cmdgen
from pysnmp.smi import builder
from pysnmp import debug

import sys
import os

debug.setLogger(debug.Debug('mibbuild'))
try:
    print sys._MEIPASS
    print os.listdir(sys._MEIPASS + '/pysnmp_mibs' )
except:
    pass

cmdGen = cmdgen.CommandGenerator()


errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd(
    cmdgen.CommunityData('public'),
    cmdgen.UdpTransportTarget(('localhost', 161)),
    cmdgen.MibVariable('SNMPv2-MIB', 'sysDescr', 0),
    lookupNames=True, lookupValues=True
)

# Check for errors and print out results
if errorIndication:
    print(errorIndication)
elif errorStatus:
    print(errorStatus)
else:
    for name, val in varBinds:
        print('%s = %s' % (name.prettyPrint(), val.prettyPrint()))

Here is the output. Notice that I added debuging flags to pysnmp to see what is going on. Since I can see the files in the directory listing above, I don't understand why pysnmp doesn't see them.

/tmp/_MEIDt6IjI
['SNMP-FRAMEWORK-MIB.pyc', 'SNMP-COMMUNITY-MIB.pyc', 'SNMP-USM-AES-MIB.py', 'SNMP-TARGET-MIB.pyc', 'SNMP-PROXY-MIB.pyc', 'TRANSPORT-ADDRESS-MIB.pyc', 'SNMPv2-MIB.pyc', 'SNMP-USER-BASED-SM-MIB.py', 'instances', 'SNMPv2-TC.pyc', 'PYSNMP-USM-MIB.py', 'ASN1.py', 'PYSNMP-MIB.py', 'SNMPv2-TC.py', 'SNMP-TARGET-MIB.py', 'SNMP-USM-AES-MIB.pyc', 'SNMP-PROXY-MIB.py', 'TRANSPORT-ADDRESS-MIB.py', 'ASN1-ENUMERATION.pyc', 'SNMP-FRAMEWORK-MIB.py', 'SNMP-VIEW-BASED-ACM-MIB.py', 'SNMPv2-CONF.pyc', 'SNMP-NOTIFICATION-MIB.py', 'SNMPv2-TM.py', 'SNMP-MPD-MIB.pyc', 'SNMP-COMMUNITY-MIB.py', 'ASN1-ENUMERATION.py', 'ASN1-REFINEMENT.py', 'PYSNMP-MIB.pyc', 'SNMP-USER-BASED-SM-MIB.pyc', 'SNMP-NOTIFICATION-MIB.pyc', 'SNMP-VIEW-BASED-ACM-MIB.pyc', 'ASN1-REFINEMENT.pyc', 'SNMP-MPD-MIB.py', 'SNMP-USER-BASED-SM-3DES-MIB.py', 'PYSNMP-USM-MIB.pyc', 'SNMPv2-TM.pyc', 'SNMPv2-SMI.py', 'SNMPv2-CONF.py', '__init__.pyo', 'ASN1.pyc', 'SNMP-USER-BASED-SM-3DES-MIB.pyc', '__init__.py', 'SNMPv2-SMI.pyc', '__init__.pyc', 'SNMPv2-MIB.py']
DBG: [15:25:14.250]: trying ZipMibSource('pysnmp.smi.mibs.instances')
DBG: [15:25:14.250]: trying ZipMibSource('pysnmp.smi.mibs')
DBG: [15:25:14.250]: trying ZipMibSource('pysnmp_mibs')
DBG: [15:25:14.250]: trying DirMibSource('pysnmp.smi.mibs.instances')
DBG: [15:25:14.250]: trying DirMibSource('pysnmp.smi.mibs')
DBG: [15:25:14.251]: trying DirMibSource('/tmp/_MEIDt6IjI/pysnmp_mibs')
DBG: [15:25:14.251]: setMibPath: new MIB sources [DirMibSource('pysnmp.smi.mibs.instances'), DirMibSource('pysnmp.smi.mibs'), DirMibSource('/tmp/_MEIDt6IjI/pysnmp_mibs')]
DBG: [15:25:14.251]: loadModules: trying SNMPv2-MIB at DirMibSource('pysnmp.smi.mibs.instances')
DBG: [15:25:14.251]: file SNMPv2-MIB.pyc mtime -1
DBG: [15:25:14.251]: file SNMPv2-MIB.py mtime -1
DBG: [15:25:14.251]: loadModules: read SNMPv2-MIB from DirMibSource('pysnmp.smi.mibs.instances') failed: No suitable module found
DBG: [15:25:14.251]: loadModules: trying SNMPv2-MIB at DirMibSource('pysnmp.smi.mibs')
DBG: [15:25:14.251]: file SNMPv2-MIB.pyc mtime -1
DBG: [15:25:14.251]: file SNMPv2-MIB.py mtime -1
DBG: [15:25:14.251]: loadModules: read SNMPv2-MIB from DirMibSource('pysnmp.smi.mibs') failed: No suitable module found
DBG: [15:25:14.251]: loadModules: trying SNMPv2-MIB at DirMibSource('/tmp/_MEIDt6IjI/pysnmp_mibs')
DBG: [15:25:14.251]: file SNMPv2-MIB.pyc mtime -1
DBG: [15:25:14.251]: file SNMPv2-MIB.py mtime -1
DBG: [15:25:14.251]: loadModules: read SNMPv2-MIB from DirMibSource('/tmp/_MEIDt6IjI/pysnmp_mibs') failed: No suitable module found
Traceback (most recent call last):
  File "<string>", line 16, in <module>
  File "/home/robertja/pysnmp/build/app/out00-PYZ.pyz/pysnmp.entity.rfc3413.oneliner.cmdgen", line 398, in __init__
  File "/home/robertja/pysnmp/build/app/out00-PYZ.pyz/pysnmp.entity.rfc3413.oneliner.cmdgen", line 36, in __init__
  File "/home/robertja/pysnmp/build/app/out00-PYZ.pyz/pysnmp.entity.engine", line 18, in __init__
  File "/home/robertja/pysnmp/build/app/out00-PYZ.pyz/pysnmp.proto.rfc3412", line 24, in __init__
  File "/home/robertja/pysnmp/build/app/out00-PYZ.pyz/pysnmp.smi.builder", line 270, in loadModules
pysnmp.smi.error.SmiError: MIB file "SNMPv2-MIB.py[co]" not found in search path

Modified the spec file to 1) import PyInstaller.hooks.hookutils 2) change hidden imports from none to hiddenimports=PyInstaller.hooks.hookutils.collect_submodules('pysnmp.smi.mibs') 3) remove the Tree and the x variable going to the EXE class

My new output

pysnmp.smi.error.SmiError: MIB module "/tmp/_MEIrjruM6/pysnmp_mibs/SNMPv2-MIB.py" load error: ['Traceback (most recent call last):\n',
'  File "/home/robertja/pysnmp/build/app/out00-PYZ.pyz/pysnmp.smi.builder", line 255, in loadModules\n', 
'  File "<string>", line 10, in <module>\n', 
'  File "/home/robertja/pysnmp/build/app/out00-PYZ.pyz/pysnmp.smi.builder", line 299, in importSymbols\n', 
'  File "/home/robertja/pysnmp/build/app/out00-PYZ.pyz/pysnmp.smi.builder", line 259, in loadModules\n', 
'SmiError: MIB module "/tmp/_MEIrjruM6/pysnmp_mibs/SNMPv2-CONF.py" load error: [\'Traceback (most recent call last):\\n\', \
'  File "/home/robertja/pysnmp/build/app/out00-PYZ.pyz/pysnmp.smi.builder", line 255, in loadModules\\n\', \
'  File "<string>", line 2, in <module>\\n\', \'  File "/home/robertja/pysnmp/build/app/out00-PYZ.pyz/pysnmp.smi.builder", line 299, in importSymbols\\n\', \
'  File "/home/robertja/pysnmp/build/app/out00-PYZ.pyz/pysnmp.smi.builder", line 259, in loadModules\\n\', \
'SmiError: MIB module "/tmp/_MEIrjruM6/pysnmp_mibs/SNMPv2-SMI.py" load error: [\\\'Traceback (most recent call last):\\\\n\\\', \\\
'  File "/home/robertja/pysnmp/build/app/out00-PYZ.pyz/pysnmp.smi.builder", line 255, in loadModules\\\\n\\\', \\\'  File "<string>", line 4, in <module>\\\\n\\\', \\\'ImportError: cannot import name exval\\\\n\\\']\\n\']\

Might as well let you see parts of the contents of the archive ArchiveViewer.py -rl dist/app

 'pysnmp.smi.mibs': (True, 585039, 109),
 'pysnmp.smi.mibs.ASN1': (False, 1038123, 298),
 'pysnmp.smi.mibs.ASN1-ENUMERATION': (False, 1038537, 235),
 'pysnmp.smi.mibs.ASN1-REFINEMENT': (False, 728848, 311),
 'pysnmp.smi.mibs.PYSNMP-MIB': (False, 312989, 1105),
 'pysnmp.smi.mibs.PYSNMP-USM-MIB': (False, 305026, 2184),
 'pysnmp.smi.mibs.SNMP-COMMUNITY-MIB': (False, 472110, 4509),
 'pysnmp.smi.mibs.SNMP-FRAMEWORK-MIB': (False, 468557, 3553),
 'pysnmp.smi.mibs.SNMP-MPD-MIB': (False, 262409, 2294),
 'pysnmp.smi.mibs.SNMP-NOTIFICATION-MIB': (False, 850428, 5070),
 'pysnmp.smi.mibs.SNMP-PROXY-MIB': (False, 68657, 3339),
 'pysnmp.smi.mibs.SNMP-TARGET-MIB': (False, 307210, 5779),
 'pysnmp.smi.mibs.SNMP-USER-BASED-SM-3DES-MIB': (False, 492595, 458),
 'pysnmp.smi.mibs.SNMP-USER-BASED-SM-MIB': (False, 1001996, 7443),
 'pysnmp.smi.mibs.SNMP-USM-AES-MIB': (False, 1046041, 1286),
 'pysnmp.smi.mibs.SNMP-VIEW-BASED-ACM-MIB': (False, 966683, 7712),
 'pysnmp.smi.mibs.SNMPv2-CONF': (False, 1137159, 972),
 'pysnmp.smi.mibs.SNMPv2-MIB': (False, 1122927, 7439),
 'pysnmp.smi.mibs.SNMPv2-SMI': (False, 889931, 11853),
 'pysnmp.smi.mibs.SNMPv2-TC': (False, 505765, 4187),
 'pysnmp.smi.mibs.SNMPv2-TM': (False, 55359, 1635),
 'pysnmp.smi.mibs.TRANSPORT-ADDRESS-MIB': (False, 1040618, 4771),
 'pysnmp.smi.mibs.instances': (True, 559625, 119),
 'pysnmp.smi.mibs.instances.__PYSNMP-USM-MIB': (False, 1030054, 353),
 'pysnmp.smi.mibs.instances.__SNMP-FRAMEWORK-MIB': (False, 558234, 488),
 'pysnmp.smi.mibs.instances.__SNMP-MPD-MIB': (False, 888877, 424),
 'pysnmp.smi.mibs.instances.__SNMP-TARGET-MIB': (False, 585148, 425),
 'pysnmp.smi.mibs.instances.__SNMP-USER-BASED-SM-MIB': (False, 663890, 585),
 'pysnmp.smi.mibs.instances.__SNMP-VIEW-BASED-ACM-MIB': (False, 553314, 323),
 'pysnmp.smi.mibs.instances.__SNMPv2-MIB': (False, 321131, 2000),
 'pysnmp.smi.view': (False, 524390, 3320),

 (4681355, 3591, 9117, 1, 'x', 'pysnmp_mibs/SNMP-FRAMEWORK-MIB.pyc'),
 (4684946, 4541, 13114, 1, 'x', 'pysnmp_mibs/SNMP-COMMUNITY-MIB.pyc'),
 (4689487, 1138, 2464, 1, 'x', 'pysnmp_mibs/SNMP-USM-AES-MIB.py'),
 (4690625, 5822, 17577, 1, 'x', 'pysnmp_mibs/SNMP-TARGET-MIB.pyc'),
 (4696447, 3374, 9009, 1, 'x', 'pysnmp_mibs/SNMP-PROXY-MIB.pyc'),
 (4699821, 4813, 15896, 1, 'x', 'pysnmp_mibs/TRANSPORT-ADDRESS-MIB.pyc'),
 (4704634, 7477, 22811, 1, 'x', 'pysnmp_mibs/SNMPv2-MIB.pyc'),
 (4712111, 6603, 26716, 1, 'x', 'pysnmp_mibs/SNMP-USER-BASED-SM-MIB.py'),
 (4718714, 4233, 12805, 1, 'x', 'pysnmp_mibs/SNMPv2-TC.pyc'),
 (4722947, 1733, 7286, 1, 'x', 'pysnmp_mibs/PYSNMP-USM-MIB.py'),
 (4724680, 242, 382, 1, 'x', 'pysnmp_mibs/ASN1.py'),
 (4724922, 878, 2772, 1, 'x', 'pysnmp_mibs/PYSNMP-MIB.py'),
 (4725800, 2981, 12859, 1, 'x', 'pysnmp_mibs/SNMPv2-TC.py'),
 (4728781, 4731, 19238, 1, 'x', 'pysnmp_mibs/SNMP-TARGET-MIB.py'),
 (4733512, 1321, 2239, 1, 'x', 'pysnmp_mibs/SNMP-USM-AES-MIB.pyc'),
 (4734833, 2869, 9850, 1, 'x', 'pysnmp_mibs/SNMP-PROXY-MIB.py'),
 (4737702, 3290, 15417, 1, 'x', 'pysnmp_mibs/TRANSPORT-ADDRESS-MIB.py'),
 (4740992, 270, 358, 1, 'x', 'pysnmp_mibs/ASN1-ENUMERATION.pyc'),
 (4741262, 2872, 8899, 1, 'x', 'pysnmp_mibs/SNMP-FRAMEWORK-MIB.py'),
 (4744134, 6989, 27846, 1, 'x', 'pysnmp_mibs/SNMP-VIEW-BASED-ACM-MIB.py'),
 (4751123, 1020, 5799, 1, 'x', 'pysnmp_mibs/SNMPv2-CONF.pyc'),
 (4752143, 4337, 18357, 1, 'x', 'pysnmp_mibs/SNMP-NOTIFICATION-MIB.py'),
 (4756480, 1021, 3202, 1, 'x', 'pysnmp_mibs/SNMPv2-TM.py'),
 (4757501, 2327, 5340, 1, 'x', 'pysnmp_mibs/SNMP-MPD-MIB.pyc'),
 (4759828, 3912, 14725, 1, 'x', 'pysnmp_mibs/SNMP-COMMUNITY-MIB.py'),
 (4763740, 136, 159, 1, 'x', 'pysnmp_mibs/ASN1-ENUMERATION.py'),
 (4763876, 183, 409, 1, 'x', 'pysnmp_mibs/ASN1-REFINEMENT.py'),
 (4764059, 1144, 2590, 1, 'x', 'pysnmp_mibs/PYSNMP-MIB.pyc'),
 (4765203, 7474, 25160, 1, 'x', 'pysnmp_mibs/SNMP-USER-BASED-SM-MIB.pyc'),
 (4772677, 5105, 16080, 1, 'x', 'pysnmp_mibs/SNMP-NOTIFICATION-MIB.pyc'),
 (4777782, 7746, 25005, 1, 'x', 'pysnmp_mibs/SNMP-VIEW-BASED-ACM-MIB.pyc'),
 (4785528, 348, 530, 1, 'x', 'pysnmp_mibs/ASN1-REFINEMENT.pyc'),
 (4785876, 2000, 5738, 1, 'x', 'pysnmp_mibs/SNMP-MPD-MIB.py'),
 (4787876, 420, 860, 1, 'x', 'pysnmp_mibs/SNMP-USER-BASED-SM-3DES-MIB.py'),
 (4788296, 2222, 6170, 1, 'x', 'pysnmp_mibs/PYSNMP-USM-MIB.pyc'),
 (4790518, 1677, 4282, 1, 'x', 'pysnmp_mibs/SNMPv2-TM.pyc'),
 (4792195, 7696, 43832, 1, 'x', 'pysnmp_mibs/SNMPv2-SMI.py'),
 (4799891, 448, 2128, 1, 'x', 'pysnmp_mibs/SNMPv2-CONF.py'),
 (4800339, 154, 187, 1, 'x', 'pysnmp_mibs/__init__.pyo'),
 (4800493, 334, 461, 1, 'x', 'pysnmp_mibs/ASN1.pyc'),
 (4800827, 494, 757, 1, 'x', 'pysnmp_mibs/SNMP-USER-BASED-SM-3DES-MIB.pyc'),
 (4801321, 62, 59, 1, 'x', 'pysnmp_mibs/__init__.py'),
 (4801383, 11951, 54264, 1, 'x', 'pysnmp_mibs/SNMPv2-SMI.pyc'),
 (4813334, 154, 187, 1, 'x', 'pysnmp_mibs/__init__.pyc'),
 (4813488, 6613, 28780, 1, 'x', 'pysnmp_mibs/SNMPv2-MIB.py'),
 (4820101, 266, 899, 1, 'x', 'pysnmp_mibs/instances/__SNMP-TARGET-MIB.py'),
 (4820367, 2040, 5486, 1, 'x', 'pysnmp_mibs/instances/__SNMPv2-MIB.pyc'),
 (4822407, 395, 640, 1, 'x', 'pysnmp_mibs/instances/__PYSNMP-USM-MIB.pyc'),
 (4822802, 304, 1006, 1, 'x', 'pysnmp_mibs/instances/__SNMP-FRAMEWORK-MIB.py'),
 (4823106, 523, 937, 1, 'x', 'pysnmp_mibs/instances/__SNMP-FRAMEWORK-MIB.pyc'),
 (4823629, 1480, 7507, 1, 'x', 'pysnmp_mibs/instances/__SNMPv2-MIB.py'),
 (4825109,
  407,
  1894,
  1,
  'x',
  'pysnmp_mibs/instances/__SNMP-USER-BASED-SM-MIB.py'),
 (4825516,
  622,
  1322,
  1,
  'x',
  'pysnmp_mibs/instances/__SNMP-USER-BASED-SM-MIB.pyc'),
 (4826138, 205, 622, 1, 'x', 'pysnmp_mibs/instances/__PYSNMP-USM-MIB.py'),
 (4826343,
  184,
  412,
  1,
  'x',
  'pysnmp_mibs/instances/__SNMP-VIEW-BASED-ACM-MIB.py'),
 (4826527,
  360,
  552,
  1,
  'x',
  'pysnmp_mibs/instances/__SNMP-VIEW-BASED-ACM-MIB.pyc'),
 (4826887, 258, 874, 1, 'x', 'pysnmp_mibs/instances/__SNMP-MPD-MIB.py'),
 (4827145, 62, 59, 1, 'x', 'pysnmp_mibs/instances/__init__.py'),
 (4827207, 160, 197, 1, 'x', 'pysnmp_mibs/instances/__init__.pyc'),
 (4827367, 461, 791, 1, 'x', 'pysnmp_mibs/instances/__SNMP-TARGET-MIB.pyc'),
 (4827828, 460, 786, 1, 'x', 'pysnmp_mibs/instances/__SNMP-MPD-MIB.pyc')]

3条回答
劫难
2楼-- · 2020-05-09 04:11

I had a similar problem. Here is the spec file that worked for me. Following suggestions from: PyInstaller does NOT work when including Pysnmp

# -*- mode: python -*-
import PyInstaller.utils.hooks
block_cipher = None

hiddenimports = ['pysnmp.smi.exval','pysnmp.cache']

a = Analysis(['py_clientMOD_agentSNMP_rev8.py'],
             pathex=['C:/Users/betan/Desktop/PY_MODBUS_SNMP/Executaveis'],
             binaries=[],
             datas=PyInstaller.utils.hooks.collect_data_files('pysnmp'),
             hiddenimports=PyInstaller.utils.hooks.collect_submodules('pysmi')+\
             PyInstaller.utils.hooks.collect_submodules('ply') + \
             PyInstaller.utils.hooks.collect_submodules('pyasn1') + \
             PyInstaller.utils.hooks.collect_submodules('pysnmp'),
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher)
x = Tree('C:/Python27/Lib/site-packages/pysnmp/smi/mibs',prefix='pysnmp/smi/mibs')
y = Tree('C:/Python27/Lib/site-packages/pysmi',prefix='pysmi')

pyz = PYZ(a.pure)
exe = EXE(pyz,
         a.scripts,
         a.binaries,
         a.zipfiles,
         a.datas,
         x, y,
         name='testSNMP',
         debug=False,
         strip=None,
         upx=True,
         console=True )

Make sure to change the pathex to your own and the paths arguments

查看更多
爷、活的狠高调
3楼-- · 2020-05-09 04:16

This was my first attempt. It didn't really work. Since I copied all the mib files in pysnmp_mibs, the program found them in my current directory path, but not in my program. renaming this directory broke it. Ignore this answer.

I figured it out. Changes are to the hiddenimports. Here is my spec file

# -*- mode: python -*-
import PyInstaller.hooks.hookutils
hiddenimports = ['pysnmp.smi.exval','pysnmp.cache'] + PyInstaller.hooks.hookutils.collect_submodules('pysnmp.smi.mibs') + PyInstaller.hooks.hookutils.collect_submodules('pysnmp.smi.mibs.instances')
a = Analysis(['app.py'],
             pathex=['/home/robertja/pysnmp'],
             hiddenimports=hiddenimports,
             hookspath=None,
             runtime_hooks=None,
)
pyz = PYZ(a.pure)
exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          name='app',
          debug=False,
          strip=None,
          upx=True,
          console=True )

I also needed to make a slight change to the application. This allows pysnmp to find the files it needs to open.

import sys
import os
try:
    sys.path.append(os.path.join(sys.MEIPOASS, 'out00-PYZ.pyz'))
except:
    pass
查看更多
Rolldiameter
4楼-- · 2020-05-09 04:19

This answer does work, but I don't like it for a few reasons.

  1. I pass the spec file the full path of where pysnmp mibs are installed. If this was generic, I would find them.
  2. pysnmp mib loader cannot read the normal place where pyinstaller places modules. It can read modules from directories and zip files (used for eggs). Pyinstaller uses a different format. I have to load the modules using Tree instead of using hidden imports.

I do not have to modify the system path.

Adding the following lines, allows the programmer better understanding of what pysnmp is doing when loading mibs. It must run before CommandGenerator.

from pysnmp import debug
debug.setLogger(debug.Debug('mibbuild'))

Here is the new spec file.

# -*- mode: python -*-
import PyInstaller.hooks.hookutils
hiddenimports = ['pysnmp.smi.exval','pysnmp.cache']
a = Analysis(['app.py'],
             pathex=['/home/robertja/pysnmp'],
             hiddenimports=hiddenimports,
             hookspath=None,
             runtime_hooks=None,
)
x = Tree('/home/robertja/.local/lib/python2.6/site-packages/pysnmp-4.2.5-py2.6.egg/pysnmp/smi/mibs',prefix='pysnmp/smi/mibs',excludes='.py')
pyz = PYZ(a.pure)
exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          x,
          name='app',
          debug=False,
          strip=None,
          upx=True,
          console=True )

Running the program using the debug printouts clearly shows pysnmp retrieving the mibs in the directory sys.MEIPOASS/pysnmp/mibs. This proves that it is working as intended.

查看更多
登录 后发表回答