14

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.

brian d foy
  • 129,424
  • 31
  • 207
  • 592
David
  • 9,635
  • 5
  • 62
  • 68

4 Answers4

14
  • 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.
Janus Troelsen
  • 20,267
  • 14
  • 135
  • 196
Florian Bösch
  • 27,420
  • 11
  • 48
  • 53
  • AFAIK rpython does not supersede pyrex; they are separate endeavours, and pyrex should still work fine (it would be my second suggestion, after re-writing the problem areas in C/C++). – Tony Meyer Oct 16 '08 at 08:30
  • There's also IronPython (assuming Windows or Mono), although it's questionable whether you'd get extra speed. – Tony Meyer Oct 16 '08 at 08:31
  • Links to rpython and PyPy are down. – BandGap Jan 10 '13 at 13:08
7

You can use something like py2exe to compile your python script into an exe, or Freeze for a linux binary.

see: How can I create a directly-executable cross-platform GUI app using Python?

Community
  • 1
  • 1
Howler
  • 2,252
  • 6
  • 22
  • 27
  • 1
    @Howler, py2exe won't deliver most of what the OP was asking for. The code is still running inside the Python runtime (albeit renamed) and won't run any faster than the script by itself) @David: You may want to consider rewriting the main app in C/C++ and extending w/ Python. – technomalogical Oct 15 '08 at 17:23
  • Or rather than rewriting the main app, just find the hotspots and write those in C/C++ as a Python extension. – Tony Meyer Oct 16 '08 at 08:27
  • 1
    Actually, I'm looking for a solution to inject code in a already running environment. But this code needs to be really fast, so interpretation is not an option. – David Oct 18 '08 at 15:00
2

I've had a lot of success using Cython, which is based on and extends pyrex:

Cython is a language that makes writing C extensions for the Python language as easy as Python itself. Cython is based on the well-known Pyrex, but supports more cutting edge functionality and optimizations.

The Cython language is very close to the Python language, but Cython additionally supports calling C functions and declaring C types on variables and class attributes. This allows the compiler to generate very efficient C code from Cython code.

This makes Cython the ideal language for wrapping for external C libraries, and for fast C modules that speed up the execution of Python code.

Matthew Trevor
  • 14,354
  • 6
  • 37
  • 50
1

I think you can use jython to compile python to Java bytecode, and then compile that with GCJ.

rjmunro
  • 27,203
  • 20
  • 110
  • 132