Skip to content

Instantly share code, notes, and snippets.

@pinguet62
Last active May 13, 2017 03:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pinguet62/d6ad5b51d9964b4fb7f0 to your computer and use it in GitHub Desktop.
Save pinguet62/d6ad5b51d9964b4fb7f0 to your computer and use it in GitHub Desktop.
Build Python script (.py) to Windows executable (.exe).

Python to .exe

This script build a Python script (.py) to Windows executable (.exe).

The output executable can be run:

  • regardless Windows versions,
  • without Python installation,
  • without module installation.

Table of contents

Requirements

Environment

This script not yet supported on Windows x64 :

running py2exe
error: bundle-files 1 not yet supported on win64

Dependency

This script depends only to py2exe module.

To dowload this module:

Running

Help

To show help message:

python build.py -h
python build.py --help

It shows this message:

usage: build.py [-h] [-v] scriptfile

positional arguments:
  scriptfile     the input script file

optional arguments:
  -h, --help     show this help message and exit
  -v, --version  show program's version number and exit

Input script

The script to compile is specified when calling the executable in command line, in the first argument:

python pathToBuild/build.py pathToArchivage/prog.py

To build a script (here it's script.py for this example) into the current folder:

python build.py script.py

Output executable

The executable is located in the current folder, under the same name but with exe extension (here script.exe).

#!/usr/bin/env python
# -*- coding: utf-8; -*-
'''
Create executable from Python script.
Type "--help" or "-h" to display help.
'''
__author__ = __maintainer__ = "Pinguet62"
__date__ = "05/2014"
__email__ = "pinguet62@gmail.com"
__status__ = "Release"
__version__ = "1.0"
import argparse
import distutils.core
import py2exe
import sys
import tempfile
# ----- Arguments ----
parser = argparse.ArgumentParser()
parser.add_argument("scriptfile",
help="the input script file")
parser.add_argument("-v", "--version",
action="version",
version="%(prog)s " + __version__)
args = parser.parse_args()
# ----- py2exe -------
# Init parameters
sys.argv = sys.argv[:1] + ["py2exe"]
# Build
distutils.core.setup(console=[args.scriptfile],
zipfile = None,
options = { "build": { "build_base": tempfile.gettempdir() }, # "build" folder
"py2exe": { "bundle_files": 1, # only 1 executable
"compressed": True,
"dist_dir": ".", # "dist" content folder directly here
"dll_excludes": ['w9xpopen.exe'] # unused for "Windows XP" and more
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment