Installing and using Pyinstaller

|
| By Webner

You can install it through pip using this command:
Pip install pyinstaller
Usage:
pyinstaller path_to_your_script.py
Pyinstaller

PyInstaller reads your Python program, analyzes all the imports it makes, and binds copies of those to the program. The user can launch the integrated application without having to install a Python interpreter or other modules.

After analyzing code and learning how to use all libraries and modules, pyinstaller then generates a “spec file”. A python script with extensions .spec, this file contains details of how your python program should be packaged. The first time you run pyinstaller command in your project directory, it will generate a spec file with some default values filled in it.

Finally, pyInstaller tries to create an executable from the application, bundling it with all its dependencies. When it finishes, a subfolder with the default name dist will appear in the project directory which has a .exe file to run. If it fails to run, you can check the errors on the command line that indicate what is wrong.

Sometimes it fails because pyinstaller fails to bundle a required file, like missing imports, missing data files/binaries. But we can edit the spec file and specify the missing details in it. For eg- if you want to add pandas then you would specify that as [‘pandas’]. Similarly, we can add data and binaries to include a missing image file, ini file, or binaries in it.

PyInstaller comes with lots of options for customizing your build, some of the commonly used options are:

--name
It changes the name of your executable file.
For eg- pyinstaller path_to_your_script.py –name customname
–onefile
It packages your entire application into a single executable file which you can file inside dist/ folder.
For eg- pyinstaller --onefile path_to_your_script.py
--add-data and --add-binary

If you don’t want to specify the missing data or binary files for your build inside spec file then this cmd can also be used.
For eg- pyinstaller --add-data="image1.png;img" --add-binary="libfoo.so;lib" path_to_your_script.py

Leave a Reply

Your email address will not be published. Required fields are marked *