Compile main Python program using Cython

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. Yes, Cython is intended to be used as stated – as a way of simplifying writing C/C++ extension modules for the CPython python runtime. But, as nudzo alludes to … Read more

Minimal set of files required to distribute an embed-Cython-compiled code and make it work on any machine

After further research (I tried in an empty Win 7 x64 bit VM, without any VCredist previously installed), it seems that these files are enough: the program itself, test.exe (produced by cython –embed and compilation with cl.exe) python37.dll python37.zip coming from packages named “Windows x86-64 embeddable zip file” in https://www.python.org/downloads/windows/ vcruntime140.dll, as mentioned in Can … Read more

Force NumPy ndarray to take ownership of its memory in Cython

You just have some minor errors in the interface definition. The following worked for me: from libc.stdlib cimport malloc import numpy as np cimport numpy as np np.import_array() ctypedef np.int32_t DTYPE_t cdef extern from “numpy/arrayobject.h”: void PyArray_ENABLEFLAGS(np.ndarray arr, int flags) cdef data_to_numpy_array_with_spec(void * ptr, np.npy_intp N, int t): cdef np.ndarray[DTYPE_t, ndim=1] arr = np.PyArray_SimpleNewFromData(1, &N, … Read more

ImportError after cython embed

Usually, a Python-interpreter isn’t “standalone” and in order to work it needs its standard libraries (for example ctypes (compiled) or site.py (interpreted)) and also path to other site-packages (for example numpy) must be set. Albeit it is possible to make a Python-interpter fully standalone by freezing the py-modules and merging all c-extensions (see for example … Read more

calling dot products and linear algebra operations in Cython?

Calling BLAS bundled with Scipy is “fairly” straightforward, here’s one example for calling DGEMM to compute matrix multiplication: https://gist.github.com/pv/5437087 Note that BLAS and LAPACK expect all arrays to be Fortran-contiguous (modulo the lda/b/c parameters), hence order=”F” and double[::1,:] which are required for correct functioning. Computing inverses can be similarly done by applying the LAPACK function … Read more

Cython: “fatal error: numpy/arrayobject.h: No such file or directory”

In your setup.py, the Extension should have the argument include_dirs=[numpy.get_include()]. Also, you are missing np.import_array() in your code. — Example setup.py: from distutils.core import setup, Extension from Cython.Build import cythonize import numpy setup( ext_modules=[ Extension(“my_module”, [“my_module.c”], include_dirs=[numpy.get_include()]), ], ) # Or, if you use cythonize() to make the ext_modules list, # include_dirs can be passed … Read more

Can Cython compile to an EXE?

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 … Read more