ampl / amplpy

Python API for AMPL

Home Page:https://amplpy.ampl.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to make it portable?

FireWallDragonDarkFluid opened this issue · comments

How to make amplpy or ampl file into portable or .exe file, so that others who don't have ampl installed could use my program?
Hard to find a document describing this issue.

For specifically saying, I was trying to use a package called Pyinstaller to convert the program, however this did not work. So I wonder if there's any other way to do it.

Thanks a lot.

This issue is stale because it has been open for 30 days with no activity.

Hi @FireWallDragonDarkFluid, we have just released ampltools v0.7.0 (you can upgrade with python -m pip install ampltool>=0.7.0) with a new command to build executable bundle using PyInstaller (we used PyInstaller v5.1 because the latest did not seem to be working correctly on Windows, you can install it with python -m pip install PyInstaller==5.1 --upgrade). You can now build portable executables as follows:

  1. Using for instance the Python script script.py with the following content:
from amplpy import AMPL
ampl = AMPL()
ampl.eval(r"""
param n integer > 0; # N-queens
var Row {1..n} integer >= 1 <= n;
s.t. row_attacks: alldiff ({j in 1..n} Row[j]);
s.t. diag_attacks: alldiff ({j in 1..n} Row[j]+j);
s.t. rdiag_attacks: alldiff ({j in 1..n} Row[j]-j);
""")
n = 10
ampl.param["n"] = n
ampl.solve(solver="highs")
solution = ampl.get_data("Row").to_dict()
queens = set((int(r) - 1, int(c) - 1) for c, r in solution.items())
print("Solution")
for r in range(n):
    print("".join([" Q " if (r, c) in queens else " + " for c in range(n)]))
  1. Run the command python -m ampltools.bundle script.py:
(venv) C:\Users\User\Desktop\pyinst>python -m ampltools.bundle script.py
$ C:\Users\User\Desktop\pyinst\venv\Scripts\python.exe -m PyInstaller script.py --collect-all amplpy --copy-metadata amplpy --collect-submodules amplpy --collect-datas amplpy --collect-binaries amplpy --collect-all ampltools --copy-metadata ampltools --collect-submodules ampltools --collect-datas ampltools --collect-binaries ampltools --collect-all ampl_module_base --copy-metadata ampl_module_base --collect-submodules ampl_module_base --collect-datas ampl_module_base --collect-binaries ampl_module_base --collect-all ampl_module_highs --copy-metadata ampl_module_highs --collect-submodules ampl_module_highs --collect-datas ampl_module_highs --collect-binaries ampl_module_highs --paths C:\Users\User\Desktop\pyinst\venv\lib\site-packages\amplpy\amplpython\cppinterface
2791 INFO: PyInstaller: 5.1
2791 INFO: Python: 3.10.11
2808 INFO: Platform: Windows-10-10.0.19045-SP0
2810 INFO: wrote C:\Users\User\Desktop\pyinst\script.spec
2825 INFO: UPX is not available.
4442 INFO: Determining a mapping of distributions to packages...
7264 INFO: Packages required by amplpy:
['ampltools']
7510 INFO: Packages required by ampltools:
['requests']
7699 INFO: Packages required by ampl_module_base:
[]
7880 INFO: Packages required by ampl_module_highs:
[]
7880 INFO: Extending PYTHONPATH with paths
['C:\\Users\\User\\Desktop\\pyinst',
 'C:\\Users\\User\\Desktop\\pyinst\\venv\\lib\\site-packages\\amplpy\\amplpython\\cppinterface']
8498 INFO: checking Analysis
8498 INFO: Building Analysis because Analysis-00.toc is non existent
8498 INFO: Initializing module dependency graph...
8503 INFO: Caching module graph hooks...
8539 INFO: Analyzing base_library.zip ...
15133 INFO: Caching module dependency graph...
15419 INFO: running Analysis Analysis-00.toc
15425 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
  required by C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64__qbz5n2kfra8p0\python.exe
15450 WARNING: lib not found: api-ms-win-appmodel-runtime-l1-1-0.dll dependency of C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64__qbz5n2kfra8p0\python.exe
15649 INFO: Analyzing C:\Users\User\Desktop\pyinst\script.py
18153 INFO: Analyzing hidden import 'amplpy.tests_camel.context'
18183 INFO: Analyzing hidden import 'amplpy.tests_camel.test_iterators'
18220 INFO: Analyzing hidden import 'amplpy.tests'
18235 INFO: Analyzing hidden import 'amplpy.tests_camel.test_properties'
18241 INFO: Analyzing hidden import 'amplpy.tests.test_ampl'
18286 INFO: Analyzing hidden import 'amplpy.tests_camel.test_environment'
18291 INFO: Analyzing hidden import 'amplpy.tests_camel.test_dataframe'
18318 INFO: Analyzing hidden import 'amplpy.modules.__main__'
18330 INFO: Analyzing hidden import 'amplpy.tests.test_dataframe'
18385 INFO: Analyzing hidden import 'amplpy.tests.__main__'
18483 INFO: Analyzing hidden import 'amplpy.tests_camel.__main__'
18541 INFO: Analyzing hidden import 'ampltools.tests.test_preload'
18572 INFO: Analyzing hidden import 'ampltools.tests.test_install'
18580 INFO: Analyzing hidden import 'ampltools.tests.__main__'
18607 INFO: Analyzing hidden import 'ampltools.modules.__main__'
18610 INFO: Analyzing hidden import 'ampl_module_base'
18633 INFO: Analyzing hidden import 'ampl_module_highs'
18650 INFO: Processing module hooks...
18650 INFO: Loading module hook 'hook-certifi.py' from 'C:\\Users\\User\\Desktop\\pyinst\\venv\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\stdhooks'...
18667 INFO: Loading module hook 'hook-charset_normalizer.py' from 'C:\\Users\\User\\Desktop\\pyinst\\venv\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\stdhooks'...
18678 INFO: Loading module hook 'hook-difflib.py' from 'C:\\Users\\User\\Desktop\\pyinst\\venv\\lib\\site-packages\\PyInstaller\\hooks'...
18681 INFO: Loading module hook 'hook-encodings.py' from 'C:\\Users\\User\\Desktop\\pyinst\\venv\\lib\\site-packages\\PyInstaller\\hooks'...
18899 INFO: Loading module hook 'hook-heapq.py' from 'C:\\Users\\User\\Desktop\\pyinst\\venv\\lib\\site-packages\\PyInstaller\\hooks'...
18909 INFO: Loading module hook 'hook-multiprocessing.util.py' from 'C:\\Users\\User\\Desktop\\pyinst\\venv\\lib\\site-packages\\PyInstaller\\hooks'...
18912 INFO: Loading module hook 'hook-pickle.py' from 'C:\\Users\\User\\Desktop\\pyinst\\venv\\lib\\site-packages\\PyInstaller\\hooks'...
18916 INFO: Loading module hook 'hook-platform.py' from 'C:\\Users\\User\\Desktop\\pyinst\\venv\\lib\\site-packages\\PyInstaller\\hooks'...
18926 INFO: Loading module hook 'hook-sysconfig.py' from 'C:\\Users\\User\\Desktop\\pyinst\\venv\\lib\\site-packages\\PyInstaller\\hooks'...
18936 INFO: Loading module hook 'hook-xml.py' from 'C:\\Users\\User\\Desktop\\pyinst\\venv\\lib\\site-packages\\PyInstaller\\hooks'...
19041 INFO: Looking for ctypes DLLs
19047 INFO: Analyzing run-time hooks ...
19053 INFO: Including run-time hook 'C:\\Users\\User\\Desktop\\pyinst\\venv\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_subprocess.py'
19057 INFO: Including run-time hook 'C:\\Users\\User\\Desktop\\pyinst\\venv\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_inspect.py'
19061 INFO: Including run-time hook 'C:\\Users\\User\\Desktop\\pyinst\\venv\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_pkgutil.py'
19077 INFO: Including run-time hook 'C:\\Users\\User\\Desktop\\pyinst\\venv\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_multiprocessing.py'
19100 INFO: Looking for dynamic libraries
1837 WARNING: lib not found: ampl-2.3.6.dll dependency of C:\Users\User\Desktop\pyinst\venv\lib\site-packages\_amplpython.cp310-win_amd64.pyd
21493 INFO: Looking for eggs
21493 INFO: Using Python library C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64__qbz5n2kfra8p0\python310.dll
21493 INFO: Found binding redirects:
[]
21500 INFO: Warnings written to C:\Users\User\Desktop\pyinst\build\script\warn-script.txt
21592 INFO: Graph cross-reference written to C:\Users\User\Desktop\pyinst\build\script\xref-script.html
21615 INFO: Appending 'binaries' from .spec
21618 INFO: Appending 'datas' from .spec
21700 INFO: checking PYZ
21700 INFO: Building PYZ because PYZ-00.toc is non existent
21700 INFO: Building PYZ (ZlibArchive) C:\Users\User\Desktop\pyinst\build\script\PYZ-00.pyz
22587 INFO: Building PYZ (ZlibArchive) C:\Users\User\Desktop\pyinst\build\script\PYZ-00.pyz completed successfully.
22629 INFO: checking PKG
22629 INFO: Building PKG because PKG-00.toc is non existent
22630 INFO: Building PKG (CArchive) script.pkg
22711 INFO: Building PKG (CArchive) script.pkg completed successfully.
22717 INFO: Bootloader C:\Users\User\Desktop\pyinst\venv\lib\site-packages\PyInstaller\bootloader\Windows-64bit\run.exe
22718 INFO: checking EXE
22724 INFO: Building EXE because EXE-00.toc is non existent
22725 INFO: Building EXE from EXE-00.toc
22730 INFO: Copying bootloader EXE to C:\Users\User\Desktop\pyinst\build\script\script.exe.notanexecutable
22956 INFO: Copying icon to EXE
22957 INFO: Copying icons from ['C:\\Users\\User\\Desktop\\pyinst\\venv\\lib\\site-packages\\PyInstaller\\bootloader\\images\\icon-console.ico']
23044 INFO: Writing RT_GROUP_ICON 0 resource with 104 bytes
23044 INFO: Writing RT_ICON 1 resource with 3752 bytes
23045 INFO: Writing RT_ICON 2 resource with 2216 bytes
23045 INFO: Writing RT_ICON 3 resource with 1384 bytes
23045 INFO: Writing RT_ICON 4 resource with 37019 bytes
23045 INFO: Writing RT_ICON 5 resource with 9640 bytes
23045 INFO: Writing RT_ICON 6 resource with 4264 bytes
23045 INFO: Writing RT_ICON 7 resource with 1128 bytes
23049 INFO: Copying 0 resources to EXE
23050 INFO: Embedding manifest in EXE
23052 INFO: Updating manifest in C:\Users\User\Desktop\pyinst\build\script\script.exe.notanexecutable
23103 INFO: Updating resource type 24 name 1 language 0
23108 INFO: Appending PKG archive to EXE
24238 INFO: Building EXE from EXE-00.toc completed successfully.
24246 INFO: checking COLLECT
24247 INFO: Building COLLECT because COLLECT-00.toc is non existent
24248 INFO: Building COLLECT COLLECT-00.toc
27330 INFO: Building COLLECT COLLECT-00.toc completed successfully.


Your executable is at "C:\Users\User\Desktop\pyinst\dist\script\script.exe".
  1. Run the executable:
(venv) C:\Users\User\Desktop\pyinst>C:\Users\User\Desktop\pyinst\dist\script\script.exe
HiGHS 1.6.0: optimal solution
975 simplex iterations
1 branching nodes

Objective = find a feasible point.
Solution
 +  +  +  +  +  Q  +  +  +  +
 +  Q  +  +  +  +  +  +  +  +
 +  +  +  +  +  +  +  +  +  Q
 +  +  +  +  +  +  +  Q  +  +
 +  +  +  Q  +  +  +  +  +  +
 +  +  +  +  +  +  +  +  Q  +
 Q  +  +  +  +  +  +  +  +  +
 +  +  Q  +  +  +  +  +  +  +
 +  +  +  +  Q  +  +  +  +  +
 +  +  +  +  +  +  Q  +  +  +

This works on Windows, Linux, and macOS.

That's amazing! Really thanks for the extension! Definetly will try it on my work!