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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With