How to use PyInstaller to create Python executables

Python, powerful and versatile as it is, lacks a few key capabilities out of the box. For one, Python provides no native mechanism for compiling a Python program into a standalone executable package.

To be fair, the original use case for Python never called for standalone packages. Python programs have, by and large, been run in-place on systems where a copy of the Python interpreter lived. But the surging popularity of Python has created greater demand for running Python apps on systems with no installed Python runtime.

Also on InfoWorld: 8 signs you’re doing Python right ]

Several third parties have engineered solutions for deploying standalone Python apps. The most popular solution of the bunch, and the most mature, is PyInstaller. PyInstaller doesn’t make the process of packaging a Python app to go totally painless, but it goes a long way there.

In this article we’ll explore the basics of using PyInstaller including how PyInstaller works, how to use PyInstaller to create a standalone Python executable, how to fine-tune the Python executables you create, and how to avoid some of the common pitfalls that go with using PyInstaller.

Creating a PyInstaller package

PyInstaller is a Python package, installed with pip (pip install pyinstaller). PyInstaller can be installed in your default Python installation, but it’s best to create a virtual environment for the project you want to package and install PyInstaller there.

PyInstaller works by reading your Python program, analyzing all of the imports it makes, and bundling copies of those imports with your program. PyInstaller reads in your program from its entry point. For instance, if your program’s entry point is myapp.py, you would run pyinstaller myapp.py to perform the analysis. PyInstaller can detect and automatically package many common Python packages, like NumPy, but you might need to provide hints in some cases. (More on this later.)

After analyzing your code and discovering all of the libraries and modules it uses, PyInstaller then generates a “spec file.” A Python script with the extension .spec, this file includes details about how your Python app needs to be packed up. The first time you run PyInstaller on your app, PyInstaller will generate a spec file from scratch and populate it with some sane defaults. Don’t discard this file; it’s the key to refining a PyInstaller deployment!

RECOMMENDED WHITEPAPERS

Finally, PyInstaller attempts to produce an executable from the app, bundled with all its dependencies. When it’s finished, a subfolder named dist (by default; you are free to specify a different name) will appear in the project directory. This in turn contains a directory that is your bundled app — it has an .exe file to run, along with all of the libraries and other supplemental files required.

SponsoredPost Sponsored by Huawei

Huawei Cloud Paves Way for Global Opportunities

A positive ecosystem supports growth of all participants. Huawei Cloud aims to establish an ecosystem to help partners across the globe.

All you need to do to distribute your program, then, is package up this directory as a .zip file or some other bundle. The bundle will typically need to be extracted in a directory where the user has write permissions in order to run.

pyinstaller1

IDG

A .spec file is used to create an executable with PyInstaller. Note the namespaces listed in _excludes_. These will be left out of the created bundle to save space.

#python #pyinstaller #programming

How to use PyInstaller to create Python executables
14.50 GEEK