setup_requires with Cython?

Starting from 18.0 release of setuptools (released on 2015-06-23) it is possible to specify Cython in setup_requires and pass *.pyx modules sources for regular setuptools.Extension: from setuptools import setup, Extension setup( # … setup_requires=[ # Setuptools 18.0 properly handles Cython extensions. ‘setuptools>=18.0’, ‘cython’, ], ext_modules=[ Extension( ‘mylib’, sources=[‘src/mylib.pyx’], ), ], )

Cython class AttributeError

By default cdef attributes are only accessible from within Cython. If you make it a public attribute with cdef public in front of the attribute name then Cython will generate suitable properties to be able to access it from Python. Some extra notes about related problems: If you’re getting the same error from within Cython … Read more

Add numpy.get_include() argument to setuptools without preinstalled numpy

First question, when is numpy needed? It is needed during the setup (i.e. when build_ext-funcionality is called) and in the installation, when the module is used. That means numpy should be in setup_requires and in install_requires. There are following alternatives to solve the issue for the setup: using PEP 517/518 (which is more straight forward … Read more

Fortran – Cython Workflow

Here’s a minimum working example. I used gfortran and wrote the compile commands directly into the setup file. gfunc.f90 module gfunc_module implicit none contains subroutine gfunc(x, n, m, a, b, c) double precision, intent(in) :: x integer, intent(in) :: n, m double precision, dimension(n), intent(in) :: a double precision, dimension(m), intent(in) :: b double precision, … Read more

Use generated header file from Cython

I think the problem is your misconception, that distutils will build the executable for you. That is not the case. Out goal is to use some python functionality from a C-program. Let’s do the necessary steps by ourselves: Using cython to produce hello.h and hello.c from the given pyx-file. Using the created hello.h for importing … Read more

Canonical way to generate random numbers in Cython

Big pre-answer caveat: this answer recommends using C++ because the question specifically asks for a solution that runs without the GIL. If you don’t have this requirement (and you probably don’t…) then Numpy is the simplest and easiest solution. Provided that you’re generating large amounts of numbers at a time you will find Numpy perfectly … Read more