How to Convert .py to .exe

How to Convert .py to .exe
Converting files from .py to .exe is a task more common than you’d imagine. Picture this scenario: you’ve just created a fantastic Python program and want to share it with the world. You send a friend a directory with all your scripts and tell him to give it a shot. Only first, he’ll have to install Python, and then run the program from the command line or IDLE shell.  Your friend balks, “Command line? Oh no, that’s not for me.” Or, maybe you’re an Admin that has to get everyone in the organization (yep, even the non-technical ones) to run your Python script. How can you distribute your Python code to users who expect to simply click on an executable file?

Fortunately, there are many Python utilities that help convert files from .py to .exe. They will “freeze” your code and build an executable file for you. Here we’ll consider two of the most popular options: cx_freeze and PyInstaller. Both of these tools are versatile and easy to use, but depending on your use case, you may prefer one over the other.

Installing Python

If you already have Python installed, you can skip this step. However, for those who haven’t, read on.

For this tutorial you can use ActiveState’s Python, which is built from vetted source code and regularly maintained for security clearance. You have two choices:

how to convert py to exe

How to Install Python Packages with the ActiveState Platform

  • Click the Get Started button and choose Python and the OS you’re working in. Choose the packages you’ll need for this tutorial, including cx_freeze and PyInstaller.
  • Once the runtime builds, you can download the State Tool and use it to install your runtime.

And that’s it! You now have installed Python in a virtual environment.

.py to .exe: How to Create an .exe file From a .py File

We’ll need a Python program from which to build our executable. For this example, I’m using a simple script that prints out Pascal’s Triangle, named
import time
n=int(input("Enter number of rows: "))
for i in range(n):
    for j in range(1,i):
for i in range(n):
    print("   "*(n-i),end=" ",sep=" ")
    for j in range(0,i+1):
        print('{0:6}'.format(a[i][j]),end=" ",sep=" ")

Assuming you’re using a Windows machine, all you need to do to build an .exe file from is:

  1. Open up the command line
  2. Navigate to the folder that contains the program
  3. Run pyinstaller

This creates a folder called “dist” in the folder that contains your Python code, and within that folder you’ll find your .exe file.

This process is almost as simple with cx_freeze, but requires an extra configuration step. Before running cx_freeze, you’ll need to create a file called “” that is stored in the same folder as the program. The file configures the options for cx_freeze, and can get complicated if you’re trying to do something very particular. For this example, however, it’s fairly simple:
from cx_Freeze import setup, Executable
setup(name = "pascals_triangle" ,
      version = "0.1" ,
      description = "" ,
      executables = [Executable("")])

To run cx_freeze, you can now:

  1. Navigate to the folder containing and
  2. Run the command python build  

When cx_freeze is done running the .exe file will be available in a directory within the folder that contains

Simplicity vs. Configurability

While the setup file required by cx_freeze presents an additional step in the process, it also enables great flexibility. In the setup file you can specify which modules, files, and packages to include (or exclude) from the build. You can set a target name for your executable, as well as a path to the icon that should be used to represent it. There’s also an option to set chosen environment variables upon installation. All this helps you to deliver an executable that matches your needs precisely.

Conversely, PyInstaller’s approach is to let you just freeze your code without worrying about all the options. It supports most libraries out-of-the-box, so the hassle of including dependent libraries is already taken care of. For the most common use cases, PyInstaller is all you need.  However, if you find that you have something like hidden imports to deal with, it’s much trickier to specify these requirements in PyInstaller.

Cross-Platform Compatibility

Both cx_freeze and PyInstaller are cross-platform in the sense that they can be used on any OS. However, neither of them have the ability to cross-compile. So if you’re using either tool on a Mac, you’ll be able to generate .app files but not .exe. In other words, if you need to distribute your program to users on a variety of platforms, you’ll need to use a variety of platforms to build them.

Code Obfuscation

Both cx_freeze and PyInstaller build a distribution that is free from your original source code.  However, both include compiled Python scripts (.pyc files). These could, in principle, be decompiled to reveal the logic of your code. If you want to protect your app against being reverse-engineered, you can encrypt the bytecode. PyInstaller provides a command line argument to obfuscate the bytecode with the Advanced Encryption Standard, AES256. In order to accomplish the same thing with cx_freeze, you’ll need to install an additional tool such as pyarmor or pyobfuscate.

One-file Mode

The motivation for this process is to make things easy for the users of your application. So what could be easier than giving them a single executable file to click on? Collapsing the entire directory into a single file is an option in PyInstaller, though not in cx_freeze.  If your end-users won’t be comfortable without the push-button simplicity of a single .exe file, then this feature alone makes PyInstaller the clear choice.

Converting .py to .exe: Conclusions

  Pros Cons
PyInstaller – Simple to use; no configuration required
– Built-in code obfuscation
– Can reduce directory to single executable
– More difficult to customize
– Cannot cross-compile
cx_freeze – Highly configurable
– Easy to explicitly include or exclude libraries
– Can set environment variables on installation
– More complicated to use
– Cannot cross-compile
– Executable must be distributed as part of a larger directory

For applications that rely on the most common libraries, PyInstaller will work right out-of-the-box and deliver the executable you need. Its simplicity, combined with some very useful features, make it a good first choice in most cases. However, if you find that your source code requires some more careful configuration, cx_freeze provides more options and explicit control when trying to convert .py to .exe.

Ultimately, both tools are easy to use and will convert your python code to an executable file that you can share. Whether or not your application is truly ready for distribution is up to you!

Download ActivePython - call to action

Download and install the pre-built “Python Executable” runtime  runtime environment for Windows 10, macOS, CentOS 7, or automatically build your own custom Python runtime on the ActiveState Platform

Frequently Asked Questions

How do I convert Python to .exe?
In order to more easily run Python scripts on Windows machines, you can convert Python to .exe files using either cx_freeze or PyInstaller. PyInstaller is simpler to use and a good first choice in most cases. However, cx_freeze is more configurable.

Get both cx_freeze and PyInstaller in our pre-built Python Executable environment.

Does Python convert files to .exe on its own?
No, Python creates .py files by default. The Python standard libraries do not include a way to create .exe files. However, you can use a third party library like cx_freeze or PyInstaller to convert your Python programs from .py to .exe files.

You can customize a Python runtime environment with both standard and third-party libraries/packages with a free account on the ActiveState Platform.

How do I create a Python .exe file for Windows?
If you’re using Python and want a simple way to create an executable that your Windows users only need to double click to run, you can use cx_freeze or PyInstaller to convert your Python code to a .exe file. While PyInstaller lets you freeze your code without worrying about all the options, cx_freeze lets you set environment variables upon installation.

Learn more about Python environment variables.

Can I double click a .py file to run it without converting to .exe?
No, .py files are not double clickable. They must be loaded into a Python interpreter in order to be run. If you are running Windows and want a double-clickable way to run Python programs, consider converting files to .exe files using either PyInstaller or cx_freeze.

Download our pre-built Python Executable environment, which includes both cx_freeze and PyInstaller.

Recent Posts

Scroll to Top