包含Pysnmp时,PyInstaller不起作用

时间:2015-10-05 09:13:16

标签: python-2.7 pyinstaller pysnmp

只需尝试一个官方文档网站示例:

from pysnmp.hlapi import *

errorIndication, errorStatus, errorIndex, varBinds = next(
    getCmd(SnmpEngine(),
          CommunityData('public'),
          UdpTransportTarget(('192.168.1.14', 161)),
          ContextData(),
          ObjectType(ObjectIdentity('1.3.6.1.2.1.1.1.0')))
)

if errorIndication:
    print(errorIndication)
elif errorStatus:
    print('%s at %s' % (
           errorStatus.prettyPrint(),
           errorIndex and varBinds[int(errorIndex)-1][0] or '?'
       )
    )
else:
    for varBind in varBinds:
       print(' = '.join([ x.prettyPrint() for x in varBind ]))

我们可以看到它通过Python解释器执行时确实有效:

 (Compiler)[user@machine testSNMP]$ python testSNMP.py 
 SNMPv2-MIB::sysDescr."0" = 48-port 10/100/1000 Gigabit Switch

然而,如果我试图"冻结"它与PyInstaller(PyInstaller --onefile)一旦执行它我得到以下错误:

  (Compiler)[user@machine testSNMP]$ /lab/testSNMP/dist/testSNMP
Traceback (most recent call last):
  File "<string>", line 4, in <module>
  File "/lab/testSNMP/build/testSNMP/out00-PYZ.pyz/pysnmp.entity.engine", line 83, in __init__
  File "/lab/testSNMP/build/testSNMP/out00-PYZ.pyz/pysnmp.smi.builder", line 359, in importSymbols
pysnmp.smi.error.MibNotFoundError: No module __SNMP-FRAMEWORK-MIB loaded at <pysnmp.smi.builder.MibBuilder instance at 0x179f518>

pysnmp / smi / mibs中的强制文件似乎未明确导入以供使用。这就是为什么我分阶段拆分构建过程的原因。拳头,创建规范文件。

(Compiler)[user@machine testSNMP]$ pyi-makespec --onefile getInterfaces.py
wrote /lab/testSNMP/getInterfaces.spec
now run pyinstaller.py to build the executable

然后,我已经对其进行了编辑,以便根据其他堆叠帖子(Can't get pysnmp to work with pyinstaller)中的建议导入所需文件:

# -*- 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(['testSNMP.py'],
            pathex=['/lab/testSNMP'],
            hiddenimports=hiddenimports,
            hookspath=None,
            runtime_hooks=None)
x = Tree('/virtualenvs/Compiler/lib/python2.7/site-packages/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='testSNMP',
         debug=False,
         strip=None,
         upx=True,
         console=True )

但是,一旦它被构建并执行,我就会收到另一个错误:

(Compiler)[user@machine testSNMP]$ /lab/testSNMP/dist/testSNMP
Traceback (most recent call last):
  File "<string>", line 15, in <module>
  File "/lab/testSNMP/build/testSNMP/out00-PYZ.pyz/pysnmp.hlapi.asyncore.sync.cmdgen", line 98, in getCmd
  File "/lab/testSNMP/build/testSNMP/out00-PYZ.pyz/pysnmp.hlapi.asyncore.cmdgen", line 135, in getCmd
  File "/lab/testSNMP/build/testSNMP/out00-PYZ.pyz/pysnmp.hlapi.varbinds", line 30, in makeVarBinds
  File "/lab/testSNMP/build/testSNMP/out00-PYZ.pyz/pysnmp.smi.rfc1902", line 689, in resolveWithMib
  File "/lab/testSNMP/build/testSNMP/out00-PYZ.pyz/pysnmp.smi.rfc1902", line 299, in resolveWithMib
  File "/lab/testSNMP/build/testSNMP/out00-PYZ.pyz/pysnmp.smi.compiler", line 44, in addMibCompiler
  File "/lab/testSNMP/build/testSNMP/out00-PYZ.pyz/pysmi.parser.smi", line 21, in __init__
  File "/lab/testSNMP/build/testSNMP/out00-PYZ.pyz/pysmi.lexer.smi", line 83, in __init__
  File "/lab/testSNMP/build/testSNMP/out00-PYZ.pyz/pysmi.lexer.smi", line 100, in reset
  File "/lab/testSNMP/build/testSNMP/out00-PYZ.pyz/ply.lex", line 915, in lex
  File "/lab/testSNMP/build/testSNMP/out00-PYZ.pyz/ply.lex", line 577, in validate_all
  File "/lab/testSNMP/build/testSNMP/out00-PYZ.pyz/ply.lex", line 819, in validate_rules
  File "/lab/testSNMP/build/testSNMP/out00-PYZ.pyz/ply.lex", line 830, in validate_module
  File "/lab/testSNMP/build/testSNMP/out00-PYZ.pyz/inspect", line 690, in getsourcelines
  File "/lab/testSNMP/build/testSNMP/out00-PYZ.pyz/inspect", line 538, in findsource
IOError: could not get source code

我还能做什么?提前谢谢!

3 个答案:

答案 0 :(得分:2)

为了让Jeremy的答案变得便携,您可以修改您的spec文件,如下所示:

+ import os.path
+ import pysmi
+ import pysnmp.smi.mibs

+ def module_base(module):
+   return os.path.abspath(os.path.join(module.__file__, os.pardir))

  coll = COLLECT(
      exe,
      a.binaries,
      a.zipfiles,
      a.datas,
+     Tree(module_base(pysmi), prefix='pysmi'),
+     Tree(module_base(pysnmp.smi.mibs), prefix='pysnmp/smi/mibs'),
      ...

编辑: 结果将这些添加到hiddenimports解决了我的问题:

'pysnmp.smi.mibs', 'pysnmp.smi.mibs.instances', 'pysnmp.smi.exval', 'pysnmp.cache'

答案 1 :(得分:1)

我遇到了同样的问题,并设法通过将pysmi文件包含在树中来实现它,就像pysnmp mib一样。

我的pyinstaller spec文件最终结果如下:

...

x = Tree(os.getcwd()+'/.pyenv/Lib/site-packages/pysnmp/smi/mibs',prefix='pysnmp/smi/mibs')
y = Tree(os.getcwd()+'/.pyenv/Lib/site-packages/pysmi',prefix='pysmi')

...

exe = EXE(pyz,
      a.scripts,
      a.binaries,
      a.zipfiles,
      a.datas,
      x,y,

...

虽然这有效但我实际上是通过使用不依赖于pysmi的旧版本的pysnmp v4.2.5以另一种方式解决了它

答案 2 :(得分:1)

得到了同样的错误&#34;无法获得源代码&#34;。在Pyinstaller github页面上创建了一个问题: https://github.com/pyinstaller/pyinstaller/issues/1945 解决方案是将ply包含到spec文件中,如您在上面提到的链接中所述: Can't get pysnmp to work with pyinstaller 通过添加&#34; PyInstaller.utils.hooks.collect_submodules(&#39; ply&#39;)&#34;到hiddenimports

并确保ply版本为&gt; = 3.9!

除了分析部分之外,我的spec文件看起来几乎相同:

a = Analysis(['main.py'],
         binaries=None,
         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=None,
         runtime_hooks=None,
         excludes=None,
         win_no_prefer_redirects=None,
         win_private_assemblies=None,
         cipher=block_cipher)