Search code examples
pythonpyinstallerpyomoglpkipopt

How to convert a Python Script constains Pyomo, GLPK, IPOPT to single exe app with Pyinstaller


I am trying to build a program which solves linear programming problems. I decided to use pyomo as package and glpk and ipopt as optimizers. I need to convert my Python script to single exe file. I used Pyinstaller but there is error

pyomo.common.errors.ApplicationError: No executable found for solver 'ipopt'

How can i include glpk and ipopt solvers to single exe file along with my Python script.

Thanks


Solution

  • I haven't tried it for single exe app, but there is a working solution for one directory mode of PyInstaller.

    1. Firstly you should specify a path to your solver executable in your pyomo optimization settings

      opt = pyomo.SolverFactory('ipopt', executable='ipopt.exe')

    For this case pyomo will be looking for solver executable in your current directory with your Python script being runned.

    1. Secondly, you may start compiling your script by PyInstaller.

    2. Once the compilation is finished, paste solvers executables into the directory with .exe file.

    If you are going to compile your code into single .exe file you can repeat that steps and compile the script into a single .exe. It should work with solvers executables placed in the very same directory of your single app file.

    However if you are going to compile your code into single .exe file without any other auxiliary files, you may try to play with .spec file which contains settings for PyInstaller. If you have that file, it could look like

    # -*- mode: python ; coding: utf-8 -*-
    
    
    block_cipher = None
    
    
    a = Analysis(['your_script.py'],
                 pathex=['path_to_your_script'],
                 binaries=[],
                 datas=[('vcomp140.dll', '.')],
                 hiddenimports=['pkg_resources.py2_warn','pyomo.common.plugins','pyomo.repn.util',
                                'pyomo.opt.plugins',
                                'pyomo.core.plugins',
                                'pyomo.dataportal.plugins',
                                'pyomo.duality.plugins',
                                'pyomo.checker.plugins',
                                'pyomo.repn.plugins',
                                'pyomo.pysp.plugins',
                                'pyomo.neos.plugins',
                                'pyomo.solvers.plugins',
                                'pyomo.gdp.plugins',
                                'pyomo.mpec.plugins',
                                'pyomo.dae.plugins',
                                'pyomo.bilevel.plugins',
                                'pyomo.scripting.plugins',
                                'pyomo.network.plugins',
                                'pandas._libs.skiplist'
                 ],
                 hookspath=[],
                 runtime_hooks=[],
                 excludes=[],
                 win_no_prefer_redirects=False,
                 win_private_assemblies=False,
                 cipher=block_cipher,
                 noarchive=False)
    pyz = PYZ(a.pure, a.zipped_data,
                 cipher=block_cipher)
    exe = EXE(pyz,
              a.scripts,
              [],
              exclude_binaries=True,
              name='your_script',
              debug=False,
              bootloader_ignore_signals=False,
              strip=False,
              upx=True,
              console=True )
    coll = COLLECT(exe,
                   a.binaries,
                   a.zipfiles,
                   a.datas,
                   strip=False,
                   upx=True,
                   upx_exclude=[],
                   name='your_script')
    

    So, you can try to include your solver executable files into PyInstaller import by adding them through data parameter in Analysis