Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling Python to native code? [duplicate]

Possible Duplicate:
Is it feasible to compile Python to machine code?

Is it possible to compile Python code (plus its dependencies, plus the interpreter library) into a single, native Windows executable (with nothing else bundled along with it) from a Python file? (Kind of like how the GNU compiler for Java compiles Java into a native (humongous) executable, which contains everything in true machine code.)

If so, how would I go about doing this?

(Specifically, py2exe does not do what I want -- it includes the libraries inside a separate ZIP file, and it includes the interpreter as a separate DLL.)

Note 1:

To emphasize, I'm not asking for a "self-extracting archive", an "executable packer", or some other way of 'cheating' by bundling the files inside an exe -- I'm looking for something that genuinely converts Python into a native executable, like what GCJ does for Java.

Note 2:

Only if the above isn't possible:

Is it possible to at least generate a single executable from a Python code containing the interpreter bundled along with all the library dependencies, such that the resulting executable does not need to self-extract onto the target disk before running?

In this scenario, the 'compilation' requirement is relaxed: it doesn't matter if the code is actually compiled into machine code (it could simply be embedded as a text resource into the target executable), but the result must nevertheless be a single exe file [and nothing else] that can run standalone, specifically without needing to unpack/install anything onto the target disk before running.

like image 346
user541686 Avatar asked Jan 09 '12 09:01

user541686


People also ask

Can Python be compiled to native code?

Python doesn't convert its code into machine code, something that hardware can understand. It actually converts it into something called byte code. So within python, compilation happens, but it's just not into a machine language.

Is it possible to obfuscate Python?

There is no effective way to obfuscate python such that it can not be trivially converted back to human readable. If you have code that valuable, convert it to C, or keep it on a server.

Can you compile Python into an executable?

Making an Executable file with auto-py-to-exe The first option offers a nice GUI (graphical user interface) that takes care of all the stuff necessary to convert your Python script into an executable file.

Can you compile Python to make it faster?

Compile PythonMachine code that you create when you compile code is always going to run faster than interpreted bytecode. You'll find several Python compilers available including Numpa, Nuitka, pypi and Cython.


1 Answers

Shed Skin can compile Python to C++, but only a restricted subset of it. Some aspects of Python are very difficult to compile to native code.

like image 86
Taymon Avatar answered Oct 07 '22 17:10

Taymon