[PyInstaller] Create multiple exe's in one folder

Published:  

python pyinstaller


PyInstaller makes it easy to create an exe, but I didn’t know how to create multiple exe’s in one folder, so I looked into it.

Create multiple exe files in one folder

Create multiple exe files in one folder

Environment

  • Windows 10
  • Python 3.9.0
  • PyInstaller 4.5

Method

1. Prepare python files

Prepare python sample files as appropriate.

In this case, we will use the following hello1.py and hello2.py.

# hello1.py
#
print('hello 1')
# hello2.py
#
print('hello 2')

2. Create a spec file for each

Create spec files for both hello1.py and hello2.py.

Running the following will create the files hello1.spec and hello2.spec.

pyi-makespec hello1.py
pyi-makespec hello2.py

The pyi-makespec is included with the PyInstaller installation.

If you open the spec file, you will see the following (some parts are omitted because it is long).
What’s inside is actually python code.

# hello1.spec
...

a = Analysis(['hello1.py'], ...)
pyz = PYZ(a.pure, a.zipped_data, ...)

exe = EXE(pyz,
          a.scripts, 
          ... )
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas, 
               ...)

PyInstaller builds the app based on the contents of the spec file.
By modifying this spec file in a good way, you can “create multiple exe’s in one folder”.

3. Combine two spec files into one spec file.

Create a single spec file by merging the contents of hello1.spec and hello2.spec.

Create a file named hello.spec and write the following while using the two spec files as a basis.

# hello.spec
...

hello1_a = Analysis(['hello1.py'], ...)
hello1_pyz = PYZ(hello1_a.pure, hello1_a.zipped_data, ...)
hello1_exe = EXE(hello1_pyz,
          hello1_a.scripts, 
          ...)

hello2_a = Analysis(['hello2.py'], ...)
hello2_pyz = PYZ(hello2_a.pure, hello2_a.zipped_data, ...)
hello2_exe = EXE(hello2_pyz,
          hello2_a.scripts, 
          ...)

coll = COLLECT(hello1_exe,
               hello1_a.binaries,
               hello1_a.zipfiles,
               hello1_a.datas,
               hello2_exe,
               hello2_a.binaries,
               hello2_a.zipfiles,
               hello2_a.datas,
               ...
               name='hello')

Copy and paste the Analysis, PYZ, and EXE parts, and modify the variable names so that they are not duplicated.
Copy and paste the COLLECT part as well, but only one, and change the argument.

4. Run PyInstaller

 pyinstaller hello.spec

When execution is complete, a folder named hello will be created under the dist folder, and hello1.exe and hello2.exe will be created in it.



Related Posts