How to compile a Python program into an executable binary?

Question: Question:

Is there a way to compile a program written in Python 3 into an executable binary?
Instead of just compiling to bytecode (* .pyc), I want to translate it into machine language so that a third party can run the program without installing his Python implementation.

It would be nice to be able to work standalone as much as possible, including various libraries and the source code of his FFI destination.
If there are restrictions on the operating environment (OS, etc.) of the binary itself, please add it.

Answer: Answer:

List what you know. The usage is slightly different, so you may have to choose it depending on the situation.

PyInstaller

PyInstaller is a tool for fixing Python packages and making them stand-alone. It works by collecting dependent Python scripts and including them with the Python interpreter. As of March 2018, the last update is 2018.

Compatible with Windows, macOS, Linux, FreeBSD, Solaris and AIX.

py2exe

py2exe is an extension of Python's distutils that aims to allow Python scripts to run standalone. The detailed mechanism is written here . As of March 2018, the last update is 2014.

Only compatible with Windows. There is also a homepage here .

py2app

py2app is the macOS version of py2exe. As of March 2018, the last update is 2017.

Only compatible with macOS. Here is the documentation.

cx_Freeze

cx_Freeze is a tool aimed at cross-platform doing things like px2exe and his py2app. As of March 2018, the last update is 2018.

Compatible with Windows, macOS and Linux. Here is the documentation.

bbfreeze

bbfreeze is a tool similar to PyInstaller and cx_Freeze. As of March 2018, the last update was 2014 and it is stated that it is not maintained.

Nuitka

Nuitka is one of the Python compilers. Transpile Python to C and link it with libpython to make it an executable. As of March 2018, the last update is 2018.

Compatible with Windows, macOS, Linux and FreeBSD. It also seems to support arm as well as x86 / x86_64.

Cython

Cython is a programming language designed to easily integrate extensions written in C into Python and is provided with a compiler. Unlike the ones above, it's not CPython based. Transpile Cython code, which has a syntax similar to Python, to C and compile it to create an executable file. As of March 2018, the last update is 2018.

Compatible with Windows, macOS and Linux. Here is the repository. The FAQ has instructions on how to create a standalone binary.

PyPy / RPython

PyPy is his JIT compiler for Python. Not based on CPython. For different purposes, you can compile a program written in RPython , a subset of Python, into an executable file (this mechanism is explained, for example, in this article by a third party). As of March 2018, the last update is 2018.

Specifically, you can compile using the repository pypy/rpython/bin/rpython .

# test.py
import sys

def fib(n):
  if n <= 2:
    return 1
  else:
    return fib(n-1) + fib(n-2)

def entry_point(argv):
  print fib(10)
  return 0

def target(*args):
  return entry_point, None

if __name__ == '__main__':
  entry_point(10)
$ python2 ./pypy/rpython/bin/rpython ./test.py
[translation:info] 2.7.14 (default, Sep 23 2017, 22:06:14) 
[GCC 7.2.0]
[platform:msg] Set platform with 'host' cc=None, using cc='gcc', version='Unknown'
[translation:info] Translating target as defined by ./test
[translation] translate.py configuration:
[translation] [translate]
    targetspec = ./test
[translation] translation configuration:
[translation] [translation]
    gc = incminimark
    gctransformer = framework
    list_comprehension_operations = True
    withsmallfuncsets = 5
[translation:info] Annotating&simplifying...
[b] {translation-task
starting annotate

(中略)

[Timer] Total:                         --- 16.5 s
$ ./test-c
55

Compatible with Windows , macOS and Linux.

Although it is old, the following questions have also been posted in connection with it.

Shed Skin

Shed Skin is a transpiler from Python (a subset of) to C ++. Please note that Python 3 is not supported . Also, the standard library is not completely usable. As of March 2018, the last update is 2017.

Similar questions on other sites

Scroll to Top
AllEscort