[英]Import Cython header from another package
I have a package called A
: 我有一个名为
A
的包:
A/
__init__.py
_test.pyx
I have the source for another package called B
: 我有另一个名为
B
包的来源:
B/
__init__.py
_wrapper.pxd
_other.pxd
_other.pyx
_wrapper.pxd
wraps a C library. _wrapper.pxd
包装了一个C库。 I'd like to cimport
_wrapper.pxd
in A/_test.pyx
我想
cimport
_wrapper.pxd
在A/_test.pyx
So I do this in _test.pyx
: 所以我在
_test.pyx
这样做:
from B cimport _wrapper
my setup.py
looks like this: 我的
setup.py
看起来像这样:
ext_modules = cythonize([
Extension("A._test", ["src/A/_test.pyx"], include_dirs=["/path/to/B", "."])])
When I compile I get B.pxd not found
followed by a bunch of errors. 当我编译时,我
B.pxd not found
然后是一堆错误。
How can I import a cython module from another package into my package? 如何从另一个包中导入cython模块到我的包中?
In order to cimport .pxd files from other packages you need to give your package a __init__. 为了从其他包中导入.pxd文件,您需要为包提供__init__。 pxd file.
pxd文件。 Then you can treat your package B as a cython package and do not need to worry about include paths.
然后,您可以将包B视为cython包,而不必担心包含路径。
I tried to reproduce the error like this: 我试图像这样重现错误:
setup.py
A/
__init__.py
_test.pyx
B/
__init__.py
_wrapper.pxd
_other.pxd
_other.pyx
_wrapper.pxd
from distutils.core import Extension, setup
from Cython.Build import cythonize
ext_modules = cythonize([
Extension("A._test", ["A/_test.pyx"])])
setup(ext_modules=ext_modules)
_test.pyx
from B cimport _wrapper cpdef bar(): _wrapper.foo()
setup.py
$ touch B/__init__.pxd
setup.py
A/
__init__.py
_test.pyx
B/
__init__.pxd
_wrapper.pxd
_other.pxd
_other.pyx
$ python setup.py build_ext --inplace
$ ipython
In [1]: from A import _test
In [2]: _test.bar()
42
And it did not work with this error: 它不适用于此错误:
A/_test.pyx: cannot find cimported module 'B' Compiling A/_test.pyx because it changed. [1/1] Cythonizing A/_test.pyx Error compiling Cython file: ------------------------------------------------------------ ... from B cimport _wrapper ^ ------------------------------------------------------------ A/_test.pyx:1:0: 'B.pxd' not found Error compiling Cython file: [...]
This should be what you faced. 这应该是你所面对的。
For me, adding an empty __init__.pxd file solved the problem. 对我来说,添加一个空的__init __。pxd文件解决了这个问题。 I did not need to specify an include path or have a __init__.py and used the files as listed above.
我不需要指定包含路径或使用__init__.py并使用上面列出的文件。 All files which are not shown above I kept empty.
我上面没有显示的所有文件都是空的。
$ touch B/__init__.pxd setup.py A/ __init__.py _test.pyx B/ __init__.pxd _wrapper.pxd _other.pxd _other.pyx $ python setup.py build_ext --inplace $ ipython In [1]: from A import _test In [2]: _test.bar() 42
I hope this works for you. 我希望这适合你。
If it is an option, consider moving the _wrapper.pxd to the A folder. 如果是一个选项,请考虑将_wrapper.pxd移动到A文件夹。 Then you can directly use:
然后你可以直接使用:
cimport _wrapper
I guess you would like to preserve the package structure as you have laid it out, so this is just here for completeness sake. 我想你想要保留包装结构,所以这只是为了完整起见。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.