Create standalone applications using cx_Freeze

Several alternative "freezing" modules exists, see https://wiki.python.org/moin/DistributionUtilities

cx_Freeze is considered a good option as it is cross platform and supports python 2 and 3.

Creating simple standalone applications is a three step process

  • Make the program
  • Write a setup script
  • Build the application

1) Make the program

In this example, the program is a simple interactive python console.

It is written to the file "my_program.py" via the %%file cellmagic


In [9]:
%%file "my_program.py"
from __future__ import division, print_function, absolute_import, unicode_literals
import time
import sys
import traceback
try: range = xrange; xrange = None
except NameError: pass
try: str = unicode; unicode = None
except NameError: pass
import numpy

while 1:
    input = raw_input(">>")
    if input == "quit()": break
    try:
        _return = None
        try:
            exec("_return = %s"%input, globals(), locals())
            print (_return)
        except SyntaxError:
            exec(input, globals(), locals())
    except Exception as e:
        print (str(e))
        print (traceback.format_exc())


Overwriting my_program.py

2) Write a setup script


In [24]:
%%file "setup.py"
    
from cx_Freeze import setup, Executable
setup(
name = "my_program",
version="1.0.0",
executables = [Executable("my_program.py")])


Overwriting setup.py

3) Build the application

To build the application open a console and run:

python setup.py build

Alternatively, run the cell below (output not shown) and wait until it returns (hopefully with a '0')


In [22]:
import os
print (os.system("python setup.py build"))


0

Now the application is build. You can find it in "./build/exe.../my_program.exe


In [ ]: