27

(Or, "Can PyPy/RPython be used to compile/translate Python to C/C++ without requiring the Python runtime?")

I have tried to comprehend PyPy with its RPython and its Python, its running and its compiling and its translating, and have somewhat failed.

I have a hypothetical Python project (for Windows); I would like to keep its size down, in the order of a hundred kilobytes (O.N.O.) rather than the several megabytes that using py2exe entails (after UPX). Can I use PyPy1 in any way to produce a standalone executable which does not depend on Python26.dll? If I can, does it need to follow the RPython restrictions like for only working on builtin types, or is it full Python syntax?

I do realise that if this can be done I almost certainly couldn't use C modules from Python directly.


1 (Since the time of asking, the situation has become clearer, and this part of the toolchain is more clearly branded as RPython rather than PyPy; it wasn't so in 2010.)

Chris Morgan
  • 86,207
  • 24
  • 208
  • 215
  • 1
    This will almost certainly not produce an executable as small as you want, but since it's related, I thought I'd point out that cython claims it can now build stand-alone executables: http://wiki.cython.org/EmbeddingCython – ʇsәɹoɈ Nov 23 '10 at 02:06
  • @Forest: I didn't realise Cython could do that, but due to requiring the Python runtime I think it's unlikely to get any (or at least much) smaller than py2exe + UPX. – Chris Morgan Nov 23 '10 at 02:12

1 Answers1

18

Yes, PyPy can produce standalone executables from RPython code. That means, you need to follow all the awkward RPython rules when it comes to writing code. Your Python code is completely unlikely to function out of the box and porting existing Python code is usually not fun. It won't make executables as small as C, but for example rpystone target (from pypy/translator/goal) using boehm GC is 80k on 64bit after stripping.

fijal
  • 3,190
  • 18
  • 21
  • Thanks for that. That's what I wanted to know and expected the answer would be. A secondary question related to that which I forgot to ask, is it possible to access the Windows API through PyPy in that? Would `ctypes.windll` work or would it be more complex? – Chris Morgan Nov 23 '10 at 11:15
  • ctypes doesn't work in RPython. There is a C interface, it's called rffi, grep for usages around (modules use it). There is quite a bit of windows API exposed here and there, look into source (sorry, but this is all fairly internal stuff, no docs ;-) – fijal Nov 23 '10 at 13:48