Generate C code from Python.

6,329 views
Skip to first unread message

arash

unread,
Aug 17, 2011, 9:53:04 PM8/17/11
to cython-users
HI all,
I am an Electrical engineer and new to Python culture , just learned
it for a large project. Basically my objective is to generate
executable C code from python scripts.Is Cython the right place to
look for ?

Thanks ,
Arash.

Stefan Behnel

unread,
Aug 18, 2011, 12:34:39 AM8/18/11
to cython...@googlegroups.com
arash, 18.08.2011 03:53:

> I am an Electrical engineer and new to Python culture , just learned
> it for a large project. Basically my objective is to generate
> executable C code from python scripts. Is Cython the right place to
> look for ?

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

arash

unread,
Aug 18, 2011, 1:42:00 AM8/18/11
to cython-users
Thanks for clarification. I have huge python modules(+8000 lines)
which what basically they do is having many functions for interacting
with a hardware platform via serial port by reading and writing to
hardware registers.
I use these libraries for writing custom scripts.I am writing a tool
which generates python scripts using these modules based on what user
like to do.
Eventually, I need to move all these stuff to be run in an embedded
processor on my hardware to have fine control, I just kick off the
event from PC and the rest is in hardware.So I need to convert them to
C.If I can have my scripts be converted to C by an automatic tool,
that would save me a huge time. I understand that there may be bugs
and tweaks and things to fix, but if it is possible at all , it worth
it...

Can I have my custom scripts be converted to C ? They use a lot of non-
standard modules .Some are custom written, some are opensource
modules.

Thanks ,
Arash.

arash

unread,
Aug 18, 2011, 1:51:40 PM8/18/11
to cython-users
Can anyone comment on this ? Is Cython what should I use in my case ?

Thanks ,
Arash.

Robert Bradshaw

unread,
Aug 18, 2011, 1:55:34 PM8/18/11
to cython...@googlegroups.com
On Thu, Aug 18, 2011 at 10:51 AM, arash <aza...@gmail.com> wrote:
> Can anyone comment on this ? Is Cython what should I use in my case ?

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.

arash

unread,
Aug 18, 2011, 4:43:49 PM8/18/11
to cython-users
Thank you.
I was thikning to just have the C code and make it standalone if
possible by removing dependencies to Python stuff.Use generated codes
as scratch..
To update...I am researcing PyPy.. It's 2 things.An effcient
implementation of RPython and a translator/Compiler with a translation
toolchain
more :
http://codespeak.net/pypy/dist/pypy/doc/translation.html#how-it-fits-together

Arash.

On Aug 18, 10:55 am, Robert Bradshaw <rober...@math.washington.edu>
wrote:

Stefan Behnel

unread,
Aug 18, 2011, 10:49:02 PM8/18/11
to cython...@googlegroups.com
arash, 18.08.2011 22:43:

> On Aug 18, 10:55 am, Robert Bradshaw
> wrote:

>> On Thu, Aug 18, 2011 at 10:51 AM, arash wrote:
>>> Can anyone comment on this ? Is Cython what should I use in my case ?
>>
>> 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.
>
> I was thikning to just have the C code and make it standalone if
> possible by removing dependencies to Python stuff.Use generated codes
> as scratch..
> To update...I am researcing PyPy.

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

Stefan Behnel

unread,
Aug 19, 2011, 2:34:58 PM8/19/11
to Cython-users
[fixed the citation order and replied to list]

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

Robert Bradshaw

unread,
Aug 19, 2011, 2:50:29 PM8/19/11
to cython...@googlegroups.com

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

Gabriel Jacobo

unread,
Aug 19, 2011, 3:19:03 PM8/19/11
to cython...@googlegroups.com
On Vie 19 Aug 2011 15:34:58 Stefan Behnel escribió:
> > 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.

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

Stefan Behnel

unread,
Aug 19, 2011, 3:24:43 PM8/19/11
to cython...@googlegroups.com
Robert Bradshaw, 19.08.2011 20:50:

> On Fri, Aug 19, 2011 at 11:34 AM, Stefan Behnel wrote:
>> 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.
>
> 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.)

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

Reply all
Reply to author
Forward
0 new messages