本文介绍了无法让 pysnmp 与 pyinstaller 一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图让 pyinstaller 与 pysnmp 一起工作 这是规范文件

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 )

这是程序文件

#!/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 添加了调试标志以查看发生了什么.由于我可以看到上面目录列表中的文件,我不明白为什么pysnmp没有看到它们.

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

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

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

我的新输出

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

Might as well let you see parts of the contents of the archiveArchiveViewer.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')]

推荐答案

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

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

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

我不必修改系统路径.

添加以下几行,可以让程序员更好地理解 pysnmp 在加载 mib 时在做什么.它必须在 CommandGenerator 之前运行.

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'))

这是新的规范文件.

# -*- 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.这证明它按预期工作.

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.

这篇关于无法让 pysnmp 与 pyinstaller 一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-03 00:18