Generating an executable from your python app or script

Author

Felipe Montealegre-Mora

Published

March 26, 2025

When you run your analysis in python, under the hood your computer is translating that script into machine code—essentially, into a binary file that then gets executed by the computer. One could say, an executable file. However, this executable file is never actually saved in your file system (as opposed to, e.g., compiling a piece of C++ code, in which the typical compiler’s output is a binary machine code file). In Python’s case, he executable “file,” the machine code instructions, are generated dynamically and immediately executed while the script is run, and once these instructions are not needed anymore they are erased.

Note

Here we should say that reality is never so simple: in many cases, intermediate bytecode, ie. .pyc, files are generated. These are not binary files, but they are an in-between step between Python and machine code. But for our purpose here, this won’t really affect the mental picture I’m trying to paint here.

Now, since our Python script is getting translated to machine code on the fly, we should be able to compile (double entendre meant) these machine code instructions into a binary file, an executable file. Conceptually, this is very simple: hijack the machine code instructions as they are generated by the Python script, and put them into a .exe file rather than running them. Because of this conceptual simplicity one would think that somewhere, somebody solved this problem in a way that is independent of operating system, hardware, computing environment, etc. All of those things seem to be irrelevant: for whatever computing environment you are in, for whatever operating system you are in, for whatever hardware you are in, the Python interpreter will generate machine code that runs and gives the exact output that would be expected by looking at the Python script.

This expectation could not be further away from reality.

It turns out that creating an executable file out of a Python script can be surprisingly tricky, particularly when one wishes to work outside the realm of Linux—Linux being the most commonly supported platform, and the platform with most documentation. In this post we will go over some headaches that come up when trying to approach this task within Mac and Windows computing environments. These headaches have been experienced first-hand by us, and we expect that others attempting the same thing might face similar headaches. That is the value we see in this post—we hope you can feel validated and in company in your struggles, and hopefully you can also find the solution that you need here!