Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Cython compile to an EXE?

I know what Cythons purpose is. It's to write compilable C extensions in a Python-like language in order to produce speedups in your code. What I would like to know (and can't seem to find using my google-fu) is if Cython can somehow compile into an executable format since it already seems to break python code down into C.

I already use Py2Exe, which is just a packager, but am interested in using this to compile down to something that is a little harder to unpack (Anything packed using Py2EXE can basically just be extracted using 7zip which I do not want)

It seems if this is not possible my next alternative would just be to compile all my code and load it as a module and then package that using py2exe at least getting most of my code into compiled form, right?

like image 316
ThantiK Avatar asked Apr 05 '10 23:04

ThantiK


People also ask

Does Cython compile to C?

The Cython language is a superset of Python that compiles to C, yielding performance boosts that can range from a few percent to several orders of magnitude, depending on the task at hand. For work that is bound by Python's native object types, the speedups won't be large.

Can Cython compile all Python code?

You can't, Cython is not made to compile Python nor turn it into an executable.


2 Answers

Here's the wiki page on embedding cython

Assuming you installed python to C:\Python31 and you want to use Microsoft Compiler.

smalltest1.py - is the file you want to compile.

test.exe - name of the executable.

You need to set the environmental variables for cl.

C:\Python31\python.exe C:\Python31\Scripts\cython.py smalltest1.py --embed  cl.exe  /nologo /Ox /MD /W3 /GS- /DNDEBUG -Ic:\Python31\include -Ic:\Python31\PC /Tcsmalltest1.c /link /OUT:"test.exe" /SUBSYSTEM:CONSOLE /MACHINE:X86 /LIBPATH:c:\Python31\libs /LIBPATH:c:\Python31\PCbuild 
like image 199
Aftershock Avatar answered Oct 14 '22 22:10

Aftershock


In principal it appears to be possible to do something like what you want, according to the Embedding Pyrex HOWTO. (Pyrex is effectively a previous generation of Cython.)

Hmm... that name suggests a better search than I first tried: "embedding cython" leads to this page which sounds like what you want.

like image 26
Peter Hansen Avatar answered Oct 14 '22 22:10

Peter Hansen