Skip to content

Instantly share code, notes, and snippets.

@ivansaul
Created July 26, 2021 22:59
Show Gist options
  • Save ivansaul/055c0827364001fffc347f65a5dcb01a to your computer and use it in GitHub Desktop.
Save ivansaul/055c0827364001fffc347f65a5dcb01a to your computer and use it in GitHub Desktop.
How to convert python project into an executable? | cx_freeze | python to exe

How to convert a python project into an executable?

Suppose our project has the following structure.

MyApp
  |-models
  |  |-login.kv
  |-data
  |  |-words.json
  |  |-audio.tar.gz
  |-fonts
  |  |-FredokaOne.ttf
  |-images
  |  |-gb.pngsound.png
  |  |-icon.ico
  |-main.py
  |-main.kv
  |-draw.py
  |-image.py

and depends on the following packages:

- kivy
- kivymd
- ffpyplayer
- gtts

Getting Started

  1. First things first is to install cx_Freeze.
pip install cx_Freeze
  1. Copy the following into a file named setup.py in the same folder as your project.
# https://cx-freeze.readthedocs.io/en/latest/distutils.html

import sys
from cx_Freeze import setup, Executable

includes = []

# Include your files and folders
includefiles = ['models/','data/','fonts/','images/','main.kv','draw.py','image.py']

# Exclude unnecessary packages
excludes = ['cx_Freeze','pydoc_data','setuptools','distutils','tkinter']

# Dependencies are automatically detected, but some modules need help.
packages = ['kivy','kivymd', 'ffpyplayer','gtts']    

base = None
shortcutName = None
shortcutDir = None
if sys.platform == "win32":
    base = "Win32GUI"
    shortcutName='My App'
    shortcutDir="DesktopFolder"

setup(
    name = 'MyApp',
    version = '0.1',
    description = 'Sample python app',
    author = 'your name',
    author_email = '',
    options = {'build_exe': {
        'includes': includes,
        'excludes': excludes,
        'packages': packages,
        'include_files': includefiles}
        }, 
    executables = [Executable('main.py', 
    base = base, # "Console", base, # None
    icon='images/icon.ico', 
    shortcutName = shortcutName, 
    shortcutDir = shortcutDir)]
)
  1. Lastly run.
python setup.py build

This command will create a subdirectory called build with a further subdirectory starting with the letters exe. and ending with the typical identifier for the platform that distutils uses. This allows for multiple platforms to be built without conflicts.

On Windows, you can build a simple installer containing all the files cx_Freeze includes for your application, by running the setup script as:

python setup.py bdist_msi

Cx_freeze references

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment