Is it possible to compile a program written in Python? [closed] Is it possible to compile a program written in Python? [closed] python python

Is it possible to compile a program written in Python? [closed]


I think Compiling Python Code would be a good place to start:

Python source code is automaticallycompiled into Python byte code by theCPython interpreter. Compiled code isusually stored in PYC (or PYO) files,and is regenerated when the source isupdated, or when otherwise necessary.

To distribute a program to people whoalready have Python installed, you canship either the PY files or the PYCfiles. In recent versions, you canalso create a ZIP archive containingPY or PYC files, and use a small“bootstrap script” to add that ZIParchive to the path.

To “compile” a Python program into anexecutable, use a bundling tool, suchas Gordon McMillan’s installer(alternative download)(cross-platform), Thomas Heller’spy2exe (Windows), Anthony Tuininga’scx_Freeze (cross-platform), or BobIppolito’s py2app (Mac). These toolsputs your modules and data files insome kind of archive file, and createsan executable that automatically setsthings up so that modules are importedfrom that archive. Some tools canembed the archive in the executableitself.


If you really want, you could always compile with Cython. This will generate C code, which you can then compile with any C compiler such as GCC.


Python, as a dynamic language, cannot be "compiled" into machine code statically, like C or COBOL can. You'll always need an interpreter to execute the code, which, by definition in the language, is a dynamic operation.

You can "translate" source code in bytecode, which is just an intermediate process that the interpreter does to speed up the load of the code, It converts text files, with comments, blank spaces, words like 'if', 'def', 'in', etc in binary code, but the operations behind are exactly the same, in Python, not in machine code or any other language. This is what it's stored in .pyc files and it's also portable between architectures.

Probably what you need it's not "compile" the code (which it's not possible) but to "embed" an interpreter (in the right architecture) with the code to allow running the code without an external installation of the interpreter. To do that, you can use all those tools like py2exe or cx_Freeze.

Maybe I'm being a little pedantic on this :-P