Jason Strimpel
Jason Strimpel

Reputation: 15496

Python/Cython: Using SciPy with Cython

The Cython tutorial shows a nice example of how to use Numpy with Cython. However, I have code that uses the scipy.stats package and when attempting to compile the code, I errors such as:

dvi.pyx:7:8: 'scipy.stats.pxd' not found

I fear that scipy is not supported with Cython (?). Can someone comment on the use of scipy with Cython or point me in the direction of some resources/tutorials? Thannks!

Upvotes: 8

Views: 10394

Answers (2)

pv.
pv.

Reputation: 35145

Write

import scipy.stats

not

cimport scipy.stats

Upvotes: 2

Jason Strimpel
Jason Strimpel

Reputation: 15496

So I found code on the Cython Google Group (https://groups.google.com/forum/?fromgroups#!searchin/cython-users/using$20scipy/cython-users/CF9GqYN1aPU/WKC-N9c6zpgJ)

That shows the following as imports:

import pylab as PL
from scipy import integrate
from scipy import optimize
from scipy.integrate import odeint

import numpy as np
cimport numpy as np
cimport cython

Which gave me confidence I could compile with SciPy. When adding the cimport cython statement, I receive the following compilation errors:

dvi.c:237:31: error: numpy/arrayobject.h: No such file or directory
dvi.c:238:31: error: numpy/ufuncobject.h: No such file or directory

Which seemed like there was a path or directory issue. In fact I was correct and there is a post on this site (My Cython code parses into C, but doesn't compile. First time trying to use external C code)

The solution was to add the following to my setup.py file:

import numpy 
...
Extension(..., include_dirs = [numpy.get_include(), ... ] )

W00t!

Upvotes: 8

Related Questions