Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cython import function from other pyx

Tags:

cython

Two pyx files,

f1.pyx

cpdef double func1():
     return 0.01

f2.pyx

from f1 cimport func1

How could I import func1 from f1.pyx? The reason is that I have different sections and want to put them in separate pyx files. But I couldn't import after I break them up.

like image 429
kaiyan711 Avatar asked Mar 19 '15 21:03

kaiyan711


People also ask

What is Cimport Cython?

The cimport statement is used in a definition or implementation file to gain access to names declared in another definition file. Its syntax exactly parallels that of the normal Python import statement. When pure python syntax is used, the same effect can be done by importing from special cython.

What is a PXD file Cython?

In addition to the . pyx source files, Cython uses . pxd files which work like C header files – they contain Cython declarations (and sometimes code sections) which are only meant for inclusion by Cython modules. A pxd file is imported into a pyx module by using the cimport keyword.


1 Answers

When you cimport a function from another file Cython needs a definition file (a *.pxd file) as well as the implementation file. (see here for the relevant section of the documentation)

If you also create a file called f1.pxd containing the following:

cpdef double func1()

Your example should compile.

like image 102
Simon Gibbons Avatar answered Oct 21 '22 00:10

Simon Gibbons