15

I have a few queries regarding python

  1. Why is there no python compiler to create native code? I have found py2exe etc but they just pack a python interpreter along with them and hence, it is again the interpreter executing the code.

  2. Is it not possible to create a python compiler like a LISP compiler and hence the code will execute faster(compared to C++)?

Thanks, Vinay

Vinay
  • 470
  • 1
  • 5
  • 18
  • 3
    You may find this question interesting: http://stackoverflow.com/questions/138521/is-it-feasible-to-compile-python-to-machine-code – charlax Jan 06 '12 at 05:53

5 Answers5

17

Nuitka – Python Compiler

What it is

I thought there ought to be possible to use a compiler for Python, a better compiler than what CPython already has with its bytecode. This is what Nuitka is supposed to be.

It is my attempt to translate pure Python not into bytecode, but into machine code (via C++ compiler), while using libpython at run time. And then to do compile time and also run time analysis to speculatively execute things in a faster mode if certain expectations are met.

Community
  • 1
  • 1
reclosedev
  • 9,352
  • 34
  • 51
  • 1
    Thank you.. This is the first time i'm hearing about this after searching for quite some time, and it seems very interesting. This was something I was looking for.. :) I have not checked how it works. – Vinay Jan 06 '12 at 10:48
9

Question 1:

  • Nuitka (Direct Python code to C++)
  • ShedSkin (Compiles implicitly statically typed Python to C++, stand-alone programs or extension modules)
  • Cython (From a superset of Python to C extensions. Cython comes from Pyrex)

Question 2:
Not sure if I get it correctly but maybe the answer is:

  • psyco (A Just in time compiler (JIT) for Python Code, the predecessor of the PyPy JIT )
joaquin
  • 82,968
  • 29
  • 138
  • 152
2

Numba is a newer Python compiler based on NumPy & LLVM, which falls back to CPython.

Liam
  • 495
  • 6
  • 21
2

The nearest equivalents for Python are cython and pypy.

Raymond Hettinger
  • 216,523
  • 63
  • 388
  • 485
2

There is, sort of.

  1. See Cython -- I haven't had a chance to fully explore it yet, but as best as I can tell, it directly compiles Python code. You can also use (optional) static typing -- it won't be vanilla Python anymore, but it can lead to a speed boost, if you do it right. Also, see this: Can Cython compile to an EXE?

  2. It might be because I don't have much experience with Lisp, but I'm not entirely sure by what you mean by 'create a Python compiler like a Lisp compiler'.

Community
  • 1
  • 1
Michael0x2a
  • 58,192
  • 30
  • 175
  • 224
  • I read somewhere there exists a LISP compiler and performance of LISP program is comparable to C++. Am I wrong? Can LISP compiler create native executable? – Vinay Jan 06 '12 at 06:18
  • Ah, I misunderstood your original question. Yes, some Lisp compilers can create native executables. Cython (which I linked above) does nearly the same thing. – Michael0x2a Jan 06 '12 at 06:35