Distributing a Python application is not a straightforward task in itself, since it requires you to take into account a large number of factors, not least the libraries involved. However, PyInstaller simplifies the process brilliantly.
If you’ve ever had the opportunity to develop with a language such as Go or Rust, you’ve probably appreciated the ability to produce a single executable binary when compiling. Distributing such an executable is simple, since there’s no need to manage resources external to the program during execution: specific components linked to an OS, graphics assets, other software…
In the vast majority of cases, the person to whom you handed such an executable simply had to run it as is.
When you’re developing in a language like Python, the compilation operation isn’t nearly as straightforward, as the executable will usually require Python to be present on the target machine, will have to be configured according to the host environment (Windows, MacOS, a particular version of Linux…), will have to integrate access to various external libraries…
💡Related articles:
What is PyInstaller?
Fortunately, Gordon McMillan, a well-known developer in the Python community, clearly saw a gap in this area. So, in 2000, he tackled the problem of how to distribute a Python script as a completely self-contained executable – without the need for a Python installation and various dependencies on the target machine. His project was named PyInstaller.
As is often the case in the Python community, other developers contributed to the project, and along the way, a real service took shape.
What does PyInstaller do?
When you submit a Python script to PyInstaller, it produces four output elements:
- An application available as a stand-alone executable (an .exe file for Windows, an .app file for MacOS, a file without extension for Linux), which can be run as is on the target machine.
- A dist (distribution) folder containing the aforementioned application and the dependencies required to run it. That’s the beauty of it: you don’t have to manage the said dependencies, PyInstaller takes care of finding them after analyzing your script and then integrating them one by one into the project, just as it takes care of integrating a Python interpreter.
- And if you’ve specified “onefile”, only the executable appears in this folder. The person running the application is often unaware that it has been developed in Python.
A build folder containing temporary files generated during the application’s creation and not required for distribution – mainly for debugging purposes. - A .spec file: this is a default configuration file for the executable, created when PyInstaller is first run on a Python script. Usually, this file needs to be customized: inclusion of certain modules, data files (e.g. images), application-specific icons, etc.
- Once this .spec file has been suitably amended, PyInstaller can be relaunched with just this .spec file to produce the desired executable application.
Why is PyInstaller so popular?
As you might imagine, PyInstaller has been a godsend for Python developers. As it turns out, it’s easy to implement, even when you’re using it for the first time. The availability of the “onefile” mode has been very positively perceived, as it produces a particularly easy-to-use executable for the user of the application. PyInstaller also has the advantage of being cross-platform: it can produce code for Windows, MacOS, GNU/Linux, as well as Unix environments such as Solaris or FreeBSD.