Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to compile Python natively (beyond pyc byte code)?

I wonder if it is possible to create an executable module from a Python script. I need to have the most performance and the flexibility of Python script, without needing to run in the Python environment. I would use this code to load on demand user modules to customize my application.

like image 546
David Avatar asked Oct 15 '08 15:10

David


People also ask

Can Python be compiled to native code?

One can write Python-to-native compilers. They just aren't very interesting because they don't actually improve performance by any significant margin, unless they actually implement a language that looks like Python but is far more restricted.

Can you run Python bytecode?

The bytecode is a low-level platform-independent representation of your source code, however, it is not the binary machine code and cannot be run by the target machine directly. In fact, it is a set of instructions for a virtual machine which is called the Python Virtual Machine (PVM).

Can you compile Python to run faster?

Although C remains the master of speed in general, PyPy can beat C in some cases. “If you want your code to magically run faster, you should probably just use PyPy.” PyPy is less effective when our program is fast anyway or when most of the runtime is spent for calls to non-python libraries.


1 Answers

  • There's pyrex that compiles python like source to python extension modules
  • rpython which allows you to compile python with some restrictions to various backends like C, LLVM, .Net etc.
  • There's also shed-skin which translates python to C++, but I can't say if it's any good.
  • PyPy implements a JIT compiler which attempts to optimize runtime by translating pieces of what's running at runtime to machine code, if you write for the PyPy interpreter that might be a feasible path.
  • The same author that is working on JIT in PyPy wrote psyco previously which optimizes python in the CPython interpreter.
like image 168
Florian Bösch Avatar answered Oct 06 '22 11:10

Florian Bösch