Running Cython in Windows x64 – fatal error C1083: Cannot open include file: ‘basetsd.h’: No such file or directory

In case anyone is currently (2017) facing same error with visual C++ 2015 tools, launch setup again and also select windows 8.1 / 10 SDK depending upon your OS. This will fix basestd.h error. If it is still not working, try launching build tools from: C:\Program Files (x86)\Microsoft Visual C++ Build Tools. Another alternative would … Read more

Using function pointers to methods of classes without the gil

You can use with gil: around the blocks that need the GIL, and then with nogil: around the important inner blocks that will take most of your run time. To give a trivial example from cython.parallel import prange cdef class Simulation: cdef double some_detail def __cinit__(self,double some_detail): self.some_detail = some_detail def do_long_calculation(self, double v): with … Read more

What does language_level in setup.py for cython do?

Building a cython extension is a two-step proccess: creating the foo.c-file from foo.pyx file using PythonX+cython-module. X could be here 2.7, 3.7 or whatever version you prefer. creating the corresponding so-file (or pyd on Windows) with help of compiler and includes of PythonY and corresponding shared library. Here Y doesn’t have to be X, but … Read more

pyclipper installation error: “tp_print is not a member of _typeobject”

The tp_print method was removed from the API in Python 3.9. Error “‘tp_print’: is not a member of ‘_typeobject’” means that the code is intended for Python <= 3.8. Downgrade to Python 3.8 and try again. Upd. pyclipper just released version 1.2.1 with wheels for Python 3.9. PS. Never hurry to a newer major version … Read more

How to profile cython functions line-by-line

Robert Bradshaw helped me to get Robert Kern’s line_profiler tool working for cdef functions and I thought I’d share the results on stackoverflow. In short, set up a regular .pyx file and build script and add the following before your call to cythonize. # Thanks to @tryptofame for proposing an updated snippet from Cython.Compiler.Options import … Read more

Calling Cython function from C code raises segmentation fault

I guess you are using Cython 0.29. Since 0.29, PEP-489 multi-phase module initialisation has been enabled for Python versions >=3.5. This means, using PyInit_XXX is no longer sufficient, as you are experiencing. Cython’s documentation suggest to use inittab mechanism, i.e. your main-function should look something like: #include “Python.h” #include “transcendentals.h” #include <math.h> #include <stdio.h> int … Read more