Python to EXE

Convert Python Code to EXE

By _Almin_ | Talking Code | 18 Apr 2021


This guide will help you convert your Python code into a standalone executable file using cx_Freeze.  I will assume that you already have a program ready to go.

Step 1
Install cx_Freeze
You can install cx_Freeze using pip
pip install cx_Freeze

Step 2
Create exe for simple programs
The only other step required to create an exe file is the following line in your terminal
cxfreeze -c YOUR_PROGRAM.py –target-dir dist –target-name NAME_OF_EXE
This will create a new folder called dist and your exe file will be inside under the name you have chosen.  If your program closes immediately, you can run it using a CLI.  Once your Python program executes each line, it closes automatically.  Running it inside of a CLI will keep the output visible until you clear it.
Creating an exe file using this method will work fine for small programs.  However, if you have a larger program, especially one that uses external libraries, you will need to spend some more time creating your exe file.

 



Converting a larger Python program into an exe file:

Step 1
Install cx_Freeze
You can install cx_Freeze using pip
pip install cx_Freeze

Step 2
Create setup.py
Create a new file in the same directory as your program and call it setup.py
Open setup.py and add the following code:

First we need to import cx_Freeze and sys:

import cx_Freeze, sys

Now we need to make sure our program runs on 32 bit and 64 bit systems:

base = None

if sys.platform == 'win32':
    base = "Win32GUI"

Next, we need to set up our executable:

executables = [cx_Freeze.Executable("YOUR_PROGRAM.py", base=base, targetName="NAME_OF_EXE")]

Now we can finish the setup (In this example we are assuming you are creating a Tkinter project that includes two pictures, called someImage and anotherImage. Any images used in your program need to be in the same directory as your program):

cx_Freeze.setup(
    name="NAME_OF_EXE",
    options={"build_exe": {"packages": ["tkinter"], "include_files": ["someImage.png", "anotherImage.png",]}},
    version="1.0",
    description="DESCRIBE YOUR PROGRAM",
    executables=executables
)

Your setup.py file should look like this:

import cx_Freeze, sys

base = None

if sys.platform == 'win32':
    base = "Win32GUI"

executables = [cx_Freeze.Executable("YOUR_PROGRAM.py", base=base, targetName="NAME_OF_EXE")]

cx_Freeze.setup(
    name="NAME_OF_EXE",
    options={"build_exe": {"packages": ["tkinter"], "include_files": ["someImage.png", "anotherImage.png",]}},
    version="1.0",
    description="DESCRIBE YOUR PROGRAM",
    executables=executables
)


Step 3
Creating the executable
Now we can run the setup file to create our executable
python setup.py build

This will create a new folder called ‘build’
Inside of the build folder, you will find another folder called ‘exe.win…’ (the actual name will vary)
Finally, inside of the exe folder you will find an application with the name you have specified in the setup file.  You can now run this application on any computer running 32-bit or 64-bit windows.  Remember that if your program closes immediately, you can run it using a CLI.  Once your Python program executes each line, it closes automatically.  Running it inside of a CLI will keep the output visible until you clear it.

If you would like to change the icon of your executable, simply add icon="ICON_NAME.ico" to your executable setup:

executables = [cx_Freeze.Executable("YOUR_PROGRAM.py", base=base, icon="ICON_NAME.ico", targetName="NAME_OF_EXE")]

...and make sure to include the file in your options:

options={"build_exe": {"packages": ["tkinter"], "include_files": ["someImage.png", "anotherImage.png", "ICON_NAME.ico",]}},


If you need more help, you can see the cx_Freeze docs

This article was originally published on Medium.  You can see it here

How do you rate this article?

5


_Almin_
_Almin_

I am a software / web developer, founder of TMD Studios, hobbyist game developer, and Bitcoin enthusiast. My work can be seen here: https://tmdstudios.github.io/


Talking Code
Talking Code

A blog that focuses on open source projects with a touch of crypto and game-dev talk

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.