无法让pysnmp使用pyinstaller

时间:2014-02-17 20:35:04

标签: python pyinstaller pysnmp

尝试让pyinstaller与pysnmp一起使用这是spec文件

# -*- 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 )

这是程序文件

#!/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()))

这是输出。请注意,我向pysnmp添加了debuging标志以查看发生了什么。 由于我可以看到上面目录列表中的文件,我不明白为什么pysnmp看不到它们。

/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

将spec文件修改为 1)导入PyInstaller.hooks.hookutils 2)将隐藏的导入从无变为 hiddenimports = PyInstaller.hooks.hookutils.collect_submodules( 'pysnmp.smi.mibs') 3)删除Tree和x变量到EXE类

我的新输出

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\']\

也可以让你看到档案内容的一部分 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')]

2 个答案:

答案 0 :(得分:1)

这是我的第一次尝试。 它没有真正起作用。由于我复制了pysnmp_mibs中的所有mib文件,程序在我当前的目录路径中找到它们,但在我的程序中找不到它们。重命名这个目录打破了它。 忽略此答案。

我明白了。改变是隐藏的进口。这是我的spec文件

# -*- 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 )

我还需要对应用程序稍作修改。这允许pysnmp找到它需要打开的文件。

import sys
import os
try:
    sys.path.append(os.path.join(sys.MEIPOASS, 'out00-PYZ.pyz'))
except:
    pass

答案 1 :(得分:0)

这个答案确实有效,但我不喜欢它有几个原因。

  1. 我将spec文件传递给pysnmp mib所在的完整路径 安装。如果这是通用的,我会找到它们。
  2. pysnmp mib loader无法读取pyinstaller放置的正常位置 模块。它可以从目录和zip文件中读取模块(使用 为了鸡蛋)。 Pyinstaller使用不同的格式。我必须使用Tree加载模块而不是使用隐藏导入。
  3. 我不必修改系统路径。

    添加以下行,允许程序员更好地理解加载mib时pysnmp正在做什么。它必须在CommandGenerator之前运行。

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

    这是新的spec文件。

    # -*- 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 )
    

    使用调试打印输出运行程序清楚地显示了pysnmp在sys.MEIPOASS / pysnmp / mibs目录中检索mib。这证明它按预期工作。