Search code examples
pythonpython-3.xgekko

Compiling Gekko with auto-py-to-exe Python


My app starts in remote="False" , m.options.SOLVER=3 mode and works fine in vscode. I needed to convert to exe file. Auto-Py-To-Exe can generate an exe file from a Python program. I managed to generate the exe from my gekko script. But when running the program, the solver shows these errors:

Error: The system cannot find the specified path.
Error: 'results.json' not found. Check above for more error details,
[Errno 2] No such file or directory: 'C:\\Users\\****\\AppData\\Local\\Temp\\tmpfzmt2vvbgk_model0\\options.json'

At runtime m.open_folder() can open the gekko path in the temp folder, but these files can't seem to be created.

In case of remote="True" and server=localhost: I try to run apmonitor server on localhost and it's ok in browser, but in code like this m = GEKKO(remote=True, server="http://127.0 .0.1") does not find the server and shows this error:

Server not found or unreachable.

In Remote="True" mode and default server: it works fine in code and exe file.

How to solve the above errors? The first mode, Remote="False" is important to me.

This is the temp folder that the resolver creates in temp:

Temp folder

UPDATE: In auto-py-to-exe and other extensions like py2exe and pyinstaller you just need to say "include Gekko data files". The auto-py-to-exe interface includes the --hidden-import and --collect-data options and must be set to from PyInstaller.utils.hooks import collect_data_files and gekko.

  • You need to install pyinstaller and auto-py-to-exe

Screenshot: auto-py-to-exe commands

In pyinstaller: Compiling Gekko with Pyinstaller


Solution

  • There are several ways to deploy a Gekko application:

    Auto-Py-To-Exe has a way to include a run directory. Instead of letting Gekko choose a temporary folder, try setting the run directory in the Python program so that it can be specified with "Add Folder" in Auto-Py-To-Exe. The animation at auto-py-to-exe on PyPI demonstrates how to add a folder.

    Auto-Py-To-Exe

    You can change the run directory by setting m._path before the m.solve() command. There is a Step by step guide on how to run Gekko optimization locally Here is a complete example where a run directory is created in the current path.

    from gekko import GEKKO
    import os
    
    # create local Gekko model
    m = GEKKO(remote=False)
    
    # get current working directory
    p = os.getcwd()
    # create run directory
    try:
        os.mkdir('run')
    except:
        pass # directory exists
    # change from tmp to new directory
    m._path = os.path.join(p,'run')
    
    # solve gekko problem
    x = m.Var(value=0)      # define new variable, initial value=0
    y = m.Var(value=1)      # define new variable, initial value=1
    m.Equations([x + 2*y==0, x**2+y**2==1]) # equations
    m.solve(disp=False)     # solve
    print([x.value[0],y.value[0]])
    
    # open run directory
    m.open_folder()