Can't get pysnmp to work with pyinstaller

百般思念 提交于 2019-12-02 11:38:46

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

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!