How to implement pyinstaller, a simple packaging tool.

Zihao Zhang
2 min readApr 6, 2021

Pyinstaller is a package used for packaging python programs into an executable file (.exe), which is easy to deliver and deploy, without an IDE.

And exe file can only (of course you can manage to run it on macOS :) ) run on Windows.

1. generate spec files

activate your virtual environment in cmd, input

pyinstaller main.py

main.py is the main function file that you want your exe to do.

Then a build and a dist file folder will be generated but can be deleted later.

And a spec file is generated too. Spec file is a Python script.

It looks like

# -*- mode: python ; coding: utf-8 -*-block_cipher = None
a = Analysis(['main.py'],
pathex=['test.py', 'C:\\Users\\******\\projects\\soot_loading\\deployment'],
binaries=[],
# datas=[('input_files.xlsx', '.'), ('GUROBI_RUN.py' , '.')],
hiddenimports=[],
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='main',
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='main')

You can either package your program as one exe file (size is greater) or as a file folder. It depends on your initial packaging command or your spec file. Above is the file folder situation.

2. Revise your spec file.

  1. put your main function in the first variable of a. The exe will run this python file.
  2. put your other py files and their path in pathx. Be noticed that if you have imported all required manual py modules in main.py, you do not have to add those py files here and the pyinstaller will package them automatically. But this does not always work and I did find some abnormal bugs.
  3. put your data files in datas. The data files will not be encrypted and will only be moved into the file folder. In other words, if they are not added in the spec files, they can just be put in the packaged file folder dist/main immediately. They both work.

3. Rerun pyinstaller in cmd

pyinstaller main.spec
  1. When packaging, some warnings will print in the cmd or in the build/main/warning.txt, You can add those modules in hidden_import. It is suggested to copy module parameters from similar spec files instead of adding them manually, which costs a lot of time.

Other problems will appear, such as in our project, There always exists a problem that No Module Named ‘GUROBI_RUN’. I have made efforts on every possible solution on the Internet including adding pathx, hidenimports, and write from *** import GORUBI_RUNin main.py, but all failed. I tried to put the file GUROBI_RUN.py in the dist/main file folder immediately, at the same level as exe file. Miraculously, it works!

4. check your main.exe

If your main.exe goes wrong, it will exit promptly. So it is suggested that run the exe program in cmd. Then if it raises an exception, the cmd will not exit immediately and you can see the print and exception logs on screen.

--

--