Why You Can’t Run Custom Code into Python-Bundled EXEs (and a Solution)
So, you’re wondering how to run custom code during the installation of an EXE generated with Python.
Python allows you to bundle binaries into a packaged EXE using tools like PyInstaller, enabling distribution as a standalone executable.
However, there is no installation process in the traditional sense. It simply packages the Python runtime and application files into a self-contained executable.
As a result, you cannot hook into the installation phase to execute custom code. Unlike complex installers (for example, MSI or EXE-based setup programs), these bundled applications do not support:
- pre-install or post-install scripts
- any registry modifications
- system-wide configurations that installers typically manage
This is happening because the Python packaging ecosystem does not currently standardize a mechanism for executing hooks during package installation. While the build process allows for hooks and plugins depending on the build backend, the installation process itself is intentionally static.
This process is designed to prevent arbitrary code execution during installation.
There have been multiple requests for post-install hooks, but these have been consistently rejected due to security concerns and the principle that installation should not execute arbitrary code.
But what do you do if your application requires it? And let’s name some of the OS integration an application might need during installation.
- Finding an available port.
- Creating an environment variable pointing to specific directories/files.
- Creating background services.
- Or executing any other script code (PowerShell, VBScript, JavaScript) during installation.
And the list could go on.
Advanced Installer, an alternative to Python-based installers
The good news is that Advanced Installer covers many functionalities. It includes some of them in the freeware edition. You can create an .msi file, which enterprise environments typically prefer.
So, if you need deeper integration with the OS and to be Enterprise environment compliant, Advanced Installer is a go-to.
To get a detailed overview of its key features, visit the Advanced Installer Freeware features page. For a practical guide on creating a simple MSI installer using this edition, consult the Creating an MSI Package step-by-step tutorial. Alternatively, you can explore the YouTube playlist specially designed for the Advanced Installer FREE Edition.
Conclusion
Python application development doesn't have to be difficult. Whether you manage a Python product for a business or run a tiny application for personal use, you may discover that using simple tools to modify certain scenarios might be challenging.
Although PyInstaller and similar tools are excellent for straightforward deployments, Advanced Installer is worth looking into if something more complicated is required.
For more aesthetically pleasing installers, think about choosing the Professional commercial editions, which let you incorporate Updater with your program or further alter the installation's theme, among many other useful features.