Have you ever wanted to quickly build your Python application into a single Windows .exe file ? or you want to bundle a large Python project into a single executable package ? Well, in this tutorial, you will learn how you can use PyInstaller package to be able to do that.
PyInstaller freezes Python applications into a stand-alone executables under Windows, GNU/Linux, Mac OS X, FreeBSD, Solaris and AIX. The user can run and execute the resulting packaged application without installing a Python interpreter or any modules. It supports Python 2.7 and Python 3.4+ and successfully bundles the major and famous Python packages such as numpy, PyQT, Django and more.
PyInstaller isn't cross-compiler, which means if you want to make a Windows executable (.exe), you need to run Pyinstaller on Windows, and if you want a GNU/Linux app, you do the same, etc.
Pyinstaller analyzes your code to discover every other module and library your script is using in order to execute, then it collects copies of all those files and puts them with your script in a single folder (or a single executable).
Read Also: How to Make a Process Monitor in Python.
Alright, it is pretty simple to install it:
pip3 install pyinstaller
To get started, I'm gonna use an example Python file from writing a keylogger tutorial, to show you how you can exactly use Pyinstaller.
So, to run it, you need to be in the same directory of the Python file you wanna build and write the following:
This will build keylogger.py file in a couple of seconds, here is my resulting folders:
As you can see, two new folders appeared ("build" and "dist"), you'll find most of the metadata and log files into build folder. As a result, we're more interested in the dist folder, after opening it up, you'll see something like this:
This packaged all the required dependencies into a single folder, in order to execute this in any Windows machine (since I built it on Windows), you need to move this entire folder and right click keylogger.exe.
If you build non-GUI applications such as this example, you'll notice that once you execute the file, a console window will appear for standard I/O, you can disable this using --noconsole, --windowed or -w parameter.
If you want to bundle everything on a single executable, you can also do that by --onefile or -F parameter which creates a one-file bundled executable.
Let's combine this two and see:
pyinstaller --noconsole --onefile keylogger.py
This will build an executable that doesn't show any console window:
A 5.5MB single executable .exe file, the file size will differ depending on the required dependencies to build this application. When I execute this, nothing happens, a stealthy application, I can only monitor it in the task manager:
You can also change the icon of the executable using -i or --icon parameter which requires a .ico, .exe or .icns file that apply its icon to this resulting Windows or Mac OS X executable.
Also, you can use some Windows specific options, such as -m or --manifest, which accepts a MANIFEST file that describe the package contents of a Windows software application.
Alright, we're done with this tutorial, use --help parameter to see various other parameters that we didn't discuss and their explanations, or you can check PyInstaller's official documentation for more detailed information.
Learn Also: How to Assemble, Disassemble and Emulate Machine Code using Python.
Happy Building ♥