Cython 构建的扩展无法导出数据类型和函数
我刚刚成功构建了我的第一个 Python C 扩展,使用 Cython 调用现有的 C 库。
我将我的数据类型和函数声明并定义为逻辑组件(遵循 C 库的逻辑结构),然后将它们合并到一个 pyx 文件中 - 当我尝试单独添加文件时发生错误(IIRC 我收到一个错误沿着已经定义的 init 行 - 在 Google 上研究这个问题后,我发现我必须将所有 pyx 文件信息合并为一个 pyx 文件) - 请参阅 此链接。
这是我的 foo.pyx 文件内容的副本:
#include "myarray.pyx"
#include "myset.pyx"
#include "mycalc.pyx"
这是我的安装文件的副本:
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
setup(
cmdclass = {'build_ext': build_ext},
ext_modules = [Extension("foo", ["foo.pyx"],
libraries=["foo_core"])
]
)
扩展已成功构建到 foo.so 中 然后我可以在 Python CLI 中输入“import foo”。这也有效。但是,当我尝试访问我在 myarray.pxd、myarray.pyx 等中声明/定义的任何类时,我收到错误消息:
AttributeError: 'module' object has no attribute 'myArray'
然后我尝试了 dir(),以查看 foo 模块导出的内容。令我惊讶的是,它列出的内容是:
>>> dir(foo)
['__builtins__', '__doc__', '__file__', '__name__', '__package__', '__test__']
Why is Cythonfailure to export the structs,classes andfunctions I'veclaimed and Defined?。我不认为我的 pxd 和 pyx 文件有什么问题,因为就像我说的,它编译成功并生成了共享库(python 扩展)。
我在 Ubuntu 上使用 Cython 0.15.1 和 Python 2.6.5
I have just managed to build my first C extension for Python, using Cython to call into an existing C library.
I declared and defined my data types and functions into logical components (following the logical structure of the C library), and I combined them into one pyx file - after errors occured when I tried to add the files individually (IIRC I got an error something along the lines of init already defined - and after researching the issue on Google, I found that I had to combine all the pyx files info one pyx file) - see this link.
This is a copy of the contents of my foo.pyx file:
#include "myarray.pyx"
#include "myset.pyx"
#include "mycalc.pyx"
and this is a copy of my setup file:
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
setup(
cmdclass = {'build_ext': build_ext},
ext_modules = [Extension("foo", ["foo.pyx"],
libraries=["foo_core"])
]
)
The extension gets built succesfully into foo.so
I can then type "import foo" at the Python CLI. That also works. However, when I try to access any of the classes I declared/defined in myarray.pxd, myarray.pyx etc, I get the error message:
AttributeError: 'module' object has no attribute 'myArray'
I then tried dir(), to see what the foo module was exporting. To my surprise, this is what it listed:
>>> dir(foo)
['__builtins__', '__doc__', '__file__', '__name__', '__package__', '__test__']
Why is Cython failing to export the structs, classes and functions I have declared and defined?. I don't think there is anything wrong with my pxd and pyx files because like I said, it compiles succesfully and the shared lib (python extendion) is produced.
I am using Cython 0.15.1 and Python 2.6.5 on Ubuntu
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
#
声明注释行的开头,因此您的foo.pyx
实际上是空的。include
是一个生硬的工具。请改用*.pxd
和 cimport。#
declares start of a comment line so yourfoo.pyx
is effectively empty.include
is a blunt instrument. Use*.pxd
and cimport instead.