Home:ALL Converter>Compile main Python program using Cython

Compile main Python program using Cython

Ask Time:2011-02-24T21:48:32         Author:mozza

Json Formatter

I have a Python2.6 program that can load Python modules compiled to .so files using Cython. I used Cython to compile the .py modules to .so files and everything works fine.

This is the setup.py file I use with Cython:

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

ext_modules = [
    Extension("ldap", ["ldap.pyx"]),
    Extension("checker", ["checker.pyx"]),
    Extension("finder", ["finder.pyx"]),
    Extension("utils", ["utils.pyx"]),
]

setup(
  name = 'bchecker',
  cmdclass = {'build_ext': build_ext},
  ext_modules = ext_modules
)

So I know I can compile Python modules using Cython (I guess Cython creates 'C' files from my Python files and then compiles them), but can I compile my main Python program to something I can execute on a Linux platform? If so, a Cython command line example would be appreciated. Thanks.

Author:mozza,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/5105482/compile-main-python-program-using-cython
Broken Man :

Contrary to what Adam Matan and others assert, you can in fact create a single executable binary file using Cython, from a pure Python (.py) file.\n\nYes, Cython is intended to be used as stated - as a way of simplifying writing C/C++ extension modules for the CPython python runtime.\n\nBut, as nudzo alludes to in this comment, you can use the --embed switch at the command line prompt.\n\nHere is an extremely simple example. I am peforming this from a Debian Sid workstation, using python3 and cython3..\n\nMake sure you have python-dev or python3-dev packages installed beforehand.\n\n1) Create a very simple Python program called hello.py\n\n\n $ cat hello.py\n \n print(\"Hello World!\")\n\n\n2) Use Cython to compile your python program into C...\n\ncython3 --embed -o hello.c hello.py\n\n\n3) Use GCC to compile hello.c into an executable file called hello...\n\ngcc -Os -I /usr/include/python3.3m -o hello hello.c -lpython3.3m -lpthread -lm -lutil -ldl\n\n\n4) You end up with a file called hello ...\n\n\n $ file hello\n \n hello: ELF 64-bit LSB executable, x86-64, version 1 (SYSV),\n dynamically linked (uses shared libs), for GNU/Linux 2.6.32,\n BuildID[sha1]=006f45195a26f1949c6ed051df9cbd4433e1ac23, not stripped\n\n\n$ ldd hello\nlinux-vdso.so.1 (0x00007fff273fe000)\nlibpython3.3m.so.1.0 => /usr/lib/x86_64-linux-gnu/libpython3.3m.so.1.0 (0x00007fc61dc2c000)\nlibpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fc61da0f000)\nlibm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fc61d70b000)\nlibutil.so.1 => /lib/x86_64-linux-gnu/libutil.so.1 (0x00007fc61d508000)\nlibdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fc61d304000)\nlibc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fc61cf5a000)\nlibrt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007fc61cd52000)\nlibexpat.so.1 => /lib/x86_64-linux-gnu/libexpat.so.1 (0x00007fc61cb28000)\nlibz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007fc61c90f000)\n/lib64/ld-linux-x86-64.so.2 (0x00007fc61e280000)\n\n\nIn this case, the executable is dynamically linked to Python 3.3 on my Debian system.\n\n5) run hello...\n\n\n $ ./hello\n \n Hello World!\n\n\nAs you can see, using this method you can basically use Cython to convert your pure Python applications into executable, compiled object code.\n\nI am using this method for vastly more complex applications - for example, a full blown Python/PySide/Qt application.\n\nFor different versions of Python, you tailor the gcc -I and -l switches to suit.\n\nYou can then package the executable as a distribution (.deb, etc.) file, without having to package the Python/PySide/Qt files - the advantage being that your application should still be able to run even after a distribution update to the same versions of Python, etc. on that distribution.",
2014-02-26T11:50:15
Scott Griffiths :

Take a look at the answers to Can Cython compile to an EXE? which, contrary to all the other answers here say that yes, it is possible to compile to an executable.\n\nThe links at Embedding Cython seem to be a good place to start, but it's not Cython's primary purpose so I don't know how straightforward it would be.",
2011-02-24T18:00:24
Jake Cook :

I don't know if this will help or not but Nudzo is correct. You can get it with cython --embed -o main.o main.py and then I try to compile the result with cl/EHsc ",
2015-12-14T12:58:52
Tim :

Look at this Post:\ncython <cython_file> --embed\n\nand then just\ngcc <C_file_from_cython> -I<include_directory> -L<directory_containing_libpython> -l<name_of_libpython_without_lib_on_the_front> -o <output_file_name>\n\nhere an example:\ncython3 main.py --embed\ngcc main.c -I /usr/include/python3.8/ -L /lib/x86_64-linux-gnu/ -l python3.8 -o main\n",
2021-10-06T13:47:34
yy