将多个子模块折叠为一个Cython扩展 [英] Collapse multiple submodules to one Cython extension

查看:225
本文介绍了将多个子模块折叠为一个Cython扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此setup.py:

from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize

extensions = (
    Extension('myext', ['myext/__init__.py',
                        'myext/algorithms/__init__.py',
                        'myext/algorithms/dumb.py',
                        'myext/algorithms/combine.py'])
)
setup(
    name='myext',
    ext_modules=cythonize(extensions)
)

没有预期的效果。我希望它生成一个单独的 myext.so ;但是当我通过

Doesn't have the intended effect. I want it to produce a single myext.so, which it does; but when I invoke it via

python -m myext.so

我得到:

ValueError: Attempted relative import in non-package

进行相对导入,原因是 myext 尝试引用 .algorithms

due to the fact that myext attempts to refer to .algorithms.

任何想法

推荐答案

首先,我应该注意它是不可能编译单个 .so 使用Cython带有子软件包的文件。因此,如果要使用子包,则必须生成多个 .so 文件,每个 .so 只能代表一个模块。

First off, I should note that it's impossible to compile a single .so file with sub packages using Cython. So if you want sub packages, you're going to have to generate multiple .so files, as each .so can only represent a single module.

第二,似乎您不能编译多个Cython / Python文件(我专门使用Cython语言)并链接

Second, it doesn't appear that you can compile multiple Cython/Python files (I'm using the Cython language specifically) and link them into a single module at all.

我试图将多个Cython文件编译为单个 .so 无论哪种方式,都使用 distutils 和手动编译,并且始终无法在运行时导入。

I've tried to compile multiply Cython files into a single .so every which way, both with distutils and with manual compilation, and it always fails to import at runtime.

它将已编译的Cython文件与其他库甚至其他C文件链接起来似乎很好,但是将两个已编译的Cython文件链接在一起时出了点问题,结果不是正确的Python扩展。

It seems that it's fine to link a compiled Cython file with other libraries, or even other C files, but something goes wrong when linking together two compiled Cython files, and the result isn't a proper Python extension.

我能看到的唯一解决方案是将所有内容编译为单个Cython文件。就我而言,我已经编辑了 setup.py 生成了一个 .pyx 文件,而该文件又反了在我的源目录中的每个 .pyx 文件中包含

The only solution I can see is to compile everything as a single Cython file. In my case, I've edited my setup.py to generate a single .pyx file which in turn includes every .pyx file in my source directory:

includesContents = ""
for f in os.listdir("src-dir"):
    if f.endswith(".pyx"):
        includesContents += "include \"" + f + "\"\n"

includesFile = open("src/extension-name.pyx", "w")
includesFile.write(includesContents)
includesFile.close()

然后我只是编译扩展名.pyx 。当然,这会破坏增量和并行编译,并且由于所有内容都粘贴到同一文件中,因此可能会导致额外的命名冲突。从好的方面来说,您不必编写任何 .pyd 文件。

Then I just compile extension-name.pyx. Of course this breaks incremental and parallel compilation, and you could end up with extra naming conflicts since everything gets pasted into the same file. On the bright side, you don't have to write any .pyd files.

我当然不会称其为首选的构建方法,但是如果所有内容都必须在一个扩展模块中,则这是我看到的唯一方法。

I certainly wouldn't call this a preferable build method, but if everything absolutely has to be in one extension module, this is the only way I can see to do it.

这篇关于将多个子模块折叠为一个Cython扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆