How to Convert Python Files into Executables

Abdou Rockikz · 28 nov 2019

Abdou Rockikz · 4 min read · General Python Topics

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 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:

pyinstaller keylogger.py

This will build keylogger.py file in a couple of seconds, here is my resulting folders:

Resulting folders after building

As you can see, two new folders appeared ("build" and "dist"), we're more interested in the dist folder, after opening it up, you'll see something like this:

Resulting executables after building

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:

Single executable resultA 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:

Executable in ProcessesYou 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. 

Alright, we're done with this tutorial, use --help parameter to see various parameters and their explanations or check PyInstaller's official documentation for more detailed information.

Learn Also: How to Send Emails in Python using smtplib Module.

Happy Building ♥

Sharing is caring!


Read Also





Comment panel

   
Comment system is still in Beta, if you find any bug, please consider contacting us here.