Absolutely. Depending on your use case (you may want to tell us *why* you
want to generate C code), there are other (more or less) Python compilers
that generate C/C++ code:
http://wiki.python.org/moin/PythonImplementations#Compilers
But arguably, Cython is the one in most widespread use and provides the
easiest manual optimisation path for getting fast code out of prototyped
Python code, even without breaking Python compatibility.
Stefan
Cython is not a Python to C compiler in the sense that it does away
with the Python interpreter/runtime, rather it creates .c files that
are compiled against the Python/C API and can be loaded into a running
session. If you can't install Python itself on your hardware, then
Cython is probably the wrong tool.
Note that PyPy's runtime dependencies aren't exactly small either
(basically: PyPy), and porting it to a new platform may or may not be
doable in an acceptable time frame.
You should also take a look at Shedskin. It only supports a statically
analysable subset of Python (and not all stdlib modules are supported), but
for what it supports, it generates very good stand-alone code (C++).
Stefan
arash, 19.08.2011 19:09:
> By cutting dependencies I menat to write my Cython or PyPy code as
> static as I can. What I understand is that at the end I have a C code
> with number of Cython/Python etc. libraries and header files beside
> standard C ones.
Ok, sure.
> I compile this and I will have object code.
Including the respective Python runtime, right.
> If this code fit in my platform memory then it's doable. Am I missing
> something ?
If you're fine with using a Python runtime and compiling your code against
it or with it, then both Cython and PyPy appear like viable (although very
different) solutions.
Stefan
Once you've gotten to this point, however, you might as well install
the CPython runtime and execute your .py files directly. (Cython and
PyPy have performance advantages, but that didn't seem to be your main
concern.)
- Robert
This is completely doable, in fact I just did something like that myself,
mixing Cython converted code as well as some frozen modules (part of the
process I've detailed in my blog at mdqinc.com)...the static binary isn't
small by any means...with SDL thrown in it climbs to about 16MB under Linux
64.
Gabriel.
www.mdqinc.com
Right, and the nice thing about Cython is that you can choose which modules
to compile and which to leave as they are, while still having access to a
huge number of available Python modules and third party extension modules.
Stefan