Use Cython as Python to C Converter

58,433

Solution 1

  1. Yes, at its core this is what Cython does. But ...
  2. You don't need Cython, however, you do need libpython. You may feel like it doesn't use that many Python features, but I think if you try this you'll find it's not true -- you won't be able to separate your program from its dependence on libpython while still using the Python language.

Another option is PyPy, specifically it's translation toolchain, NOT the PyPy Python interpreter. It lets you translate RPython, a subset of the Python language, into C. If you really aren't using many Python language features or libraries, this may work.

PyPy is mostly known as an alternative Python implementation, but it is also a set of tools for compiling dynamic languages into various forms. This is what allows the PyPy implementation of Python, written in (R)Python, to be compiled to machine code.

If C++ is available, Nuitka is a Python to C++ compiler that works for regular Python, not just RPython (which is what shedskin and PyPy use).

Solution 2

If C++ is available for that embedded platform, there is shed skin, it converts python into c++.

Share:
58,433
Admin
Author by

Admin

Updated on May 20, 2020

Comments

  • Admin
    Admin about 4 years

    I have huge Python modules(+8000 lines) .They basically have tons of functions for interacting with a hardware platform via serial port by reading and writing to hardware registers.

    They are not numerical algorithms. So application is just reading/writing to hardware registers/memory. I use these libraries to write custom scripts. Eventually, I need to move all these stuff to be run in an embedded processor on my hardware to have finer control, then 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. This is why I got attracted to Cython. Efficiency is not important my codes are not number crunchers. But generated code should be relatively small to fit in my limited memory (few hundreds of Kilobytes).

    Can I use Cython as converter for my custom Python scripts to C? My guess is yes, in which case can I use these .c files to be run in my hardware? My guess is not since I have to have Cython in my hardware as well for them to run. But if just creates some .c files, I can go through and make it standalone since code is not using much of features of Python it just use it as a fast to implement script.

  • Admin
    Admin almost 13 years
    I heard about ShedSkin but I cant trust it.Cython seems to be more robust and widely used with very positive reviews.
  • Admin
    Admin almost 13 years
    So It's a translator that gives you .c ? I thought is specific implementation of python for making it fast.Being fast is not my concern.
  • agf
    agf almost 13 years
    Read the answer more closely. PyPy is two things, an implementation of Python, and a Python translator / compiler. You don't want the first, you want the second. The translation toolchain I linked to has details on what you want.
  • Kyss Tao
    Kyss Tao about 12 years
    @agf, I cannot figure out how to translate a python program (say hello.py containing one line i=1; print hello, 1) to a c source file that I can look at in an editor and compile with gcc. I'd appreciate if you could help me with an example.
  • Kyss Tao
    Kyss Tao about 12 years
    @agf I saw this link before, I tried it out now: I only get a binary, no C source, and it does not work for my example above. Also what do you think they mean by 'PyPy cannot compile normal Python programs to C' at doc.pypy.org/en/latest/… ? That does not sound good :-(
  • agf
    agf about 12 years
    @KyssTao R Python is what PyPy can translate to C. Read the next section of that FAQ, my answer, and the other links.
  • alcalde
    alcalde over 10 years
    You don't use PyPy to translate Python to C. You use Cython, which was designed for this task. The user does not want to translate RPython to C, which would be a convoluted approach anyway while Cython offers a simple, clear path.
  • agf
    agf over 10 years
    @alcalde I've cleaned up this (very old) answer somewhat; you're right, it was misleading.
  • boardrider
    boardrider about 8 years
    For C++, there's also Pythran.
  • gvoysey
    gvoysey almost 8 years
    @agf sorry to bump a very old question: would c code generated as you describe in your answer be portable enough to run on an embedded system that would have zero python-related things installed on it --some IoT system, for example. I know this isn't true for Cython (which depends on libpython)
  • agf
    agf almost 8 years
    @gvoysey Possibly, I don't know. Keep in mind it only works for RPython, not true Python. But if your goal is to run the code on an embedded system, I don't think translating to C should be your goal. Depending on what you mean by "embedded" you may be able to use a regular Python interpreter, or Cython + libpython. If you mean a microcontroller, you may want to look into MicroPython.
  • gvoysey
    gvoysey almost 8 years
    @agf it's vworks. C is kind of the only supported game in town. If RPython/ PyPy spits out something that gcc can compile, great. If not, i guess I get my hands dirty.
  • agf
    agf almost 8 years
    @gvoysey You can't Cython to translate to C and then link it to libpython? Seems unlikely vworks wouldn't have a linker?
  • gvoysey
    gvoysey almost 8 years
    @agf i honestly have no idea. What i have to do is really simple data munging, though, so i hope I can work within rpython and emit "clean" C.