Can Cython compile to an EXE?
Asked Answered
U

4

58

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?

Udele answered 5/4, 2010 at 23:34 Comment(1)
Linked to Making an executable in CythonEctropion
T
31

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.

Testerman answered 5/4, 2010 at 23:47 Comment(1)
Please see my example of how to do exactly that here for a Linux system.Lamentation
C
48

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
Crowfoot answered 30/4, 2010 at 9:12 Comment(5)
Waw, it works very well. At the end the .exe file is really tiny. This is really a cython2exe similar to py2exe ! Could we do a script that does this automatically (adapt to the current Python version installed, etc.) ? What files do we need to ship with this .exe ?Ectropion
I am getting the errors ignoring unknown option /main.c, ` missing source filename`Jacquard
From where to get cython.py file?Sweatbox
what is cl.exe ??Gemology
Microsoft c++ compilerCrowfoot
T
31

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.

Testerman answered 5/4, 2010 at 23:47 Comment(1)
Please see my example of how to do exactly that here for a Linux system.Lamentation
M
6

I have successfully used the Cython & gcc to convert the *.py file to *.exe, with below batch file:

# build.bat
set PROJECT_NAME=test
set PYTHON_DIR=C:\python27
%PYTHON_DIR%\python -m cython --embed -o %PROJECT_NAME%.c %PROJECT_NAME%.py
gcc -Os -I %PYTHON_DIR%\include -o %PROJECT_NAME%.exe %PROJECT_NAME%.c -lpython27 -lm -L %PYTHON_DIR%\libs
Microbalance answered 30/9, 2018 at 7:36 Comment(0)
T
2

Aftershock's answer is good, what I want to say is about run app without console. Most like Aftershock's answer, if you want your application run without console, two points are important:

  1. Replace all the main() function in the ".c" file made by cython --embed with wmain()

  2. Add /subsystem:windows /entry:wmainCRTStartup to the end of cl.exe ... command

Tajo answered 21/5, 2018 at 15:50 Comment(2)
Very useful indeed! /subsystem:windows /entry:wmainCRTStartup was required indeed (1. was not necessary for me).Ectropion
How to do the same with gcc ? (mingw64)Acanthopterygian

© 2022 - 2024 — McMap. All rights reserved.