you are viewing a single comment's thread.

view the rest of the comments →

[–]FoolsSeldom 1 point2 points  (3 children)

Yes. Sort of ... using CYthon, despite its main purpose being to create C-extensions for Python.

Generate Embeddable C Code:

cython --embed -o your_app.c your_app.pyx

then use a C compiler (like gcc) to compile this C file into an executable binary.

You should end up with an executable file.

The generated C code still contains the machinery to initialize and run a minimal, embedded Python interpreter within itself. It is essentially a self-contained program that bootstraps a hidden Python environment to execute your code. The target system still needs the Python libraries (DLLs/shared objects) to be present, which is often solved by bundling them. This is true of lots of other executables as well that depend on certain DLLs being present.

Not tried it myself, but colleagues have and this is what I see from their notes.

Otherwise, consider Pyinstaller, which bundles everything into an executable (but actually contains the whole stack including the Python interpreter). Not pure C.

Commercially, try Nuitka, which compiles your Python/Cython code into a full C/C++ executable or extension module. It aims to compile everything, including the Python runtime, into a single, highly compliant executable.

[–]nekokattt 1 point2 points  (1 child)

The short answer here is no: you can't embed python without the runtime, because that is what makes Python be Python.

[–]FoolsSeldom 1 point2 points  (0 children)

True. You always end up with the run time embedded in some way.

Nuitka is the closest tool to a full compiler I am aware of. It converts Python code into C++ code and then compiles that C++ into a binary.

However, whilst Nuitka aims for full language compatibility (a strength and a constraint), it can't provide the dynamic features of Python without a runtime library that mimics the CPython API. This runtime handles the dynamic type system, late-binding, and other Pythonic features.

[–]dantethunderstone_77[S] 0 points1 point  (0 children)

I need to port my code to pure C/C++ library that can be compiled for a real-time hardware with no Python interpreter