当__init__.py存在时,带有设置的Python构建cython扩展会创建子文件夹 [英] Python building cython extension with setup creates subfolder when __init__.py exists

查看:122
本文介绍了当__init__.py存在时,带有设置的Python构建cython扩展会创建子文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下 setup.py 编译一个简单的 cython 模块:

I am trying to compile a simple cython module using the following setup.py:

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

setup(
    ext_modules=cythonize("verifier_c.pyx"),
)

我具有以下文件夹结构:

I have the following folder structure:

.
c_ext/
  __init__.py
  verifier_c.pyx
  setup.py

如果我运行以下命令:

python setup.py build_ext --inplace

我会得到一个额外的 c_ext 子文件夹,如下所示:

I get an extra c_ext subfolder like this:

.
c_ext/
  build/
    ...
  c_ext/
    verifier_c.so
  __init__.py
  verifier_c.pyx
  setup.py

但是如果我删除 __ init __。py 文件,我将 verifier_c.so 文件与 verifier_c.pyx 放在同一文件夹中。

But if I remove the __init__.py file, I get the verifier_c.so file in the same folder as verifier_c.pyx.

我没有找到记录此行为的地方,但我想将 verifier_c.so 保留在与 verifier_c.pyx 但不必每次运行 setup.py <时都删除 __ init __。py / code>。我该如何实现?

I did not find where this behavior is documented, but I would like to keep verifier_c.so in the same folder as verifier_c.pyx but without having to delete __init__.py each time I run setup.py. How can I achieve that?

推荐答案

如评论中所述, setup.py 不应该放在您的软件包中。据我所知,build_ext命令没有选项(除了-inplace 之外)来指定目标路径。您可以在此处找到一些文档。另外这个问题处理类似主题。

As mentioned in the comments, the setup.py should not live inside your package. As far as I know the build_ext commands has no option (apart from --inplace) to specify a target path. You can find some documentation here. Also this question deals with a similar topic.

要适应所需的包装结构,您的包装必须像这样:

To adapts the required package structure your package would have to look like:

c_ext/
    setup.py
    myfile.py
    verifier/
        __init__.py
        verifier_c.pyx

您将获得验证程序包中的扩展名:

You will get an extension that lives in the verifier package:

me@machine:~/c_ext/$ python setup.py build_ext --inplace

c_ext/
    setup.py
    myfile.py
    verifier/
        __init__.py
        verifier_c.pyx
        verifier_c.so

然后可以从验证程序包中导入verifier_c。例如,来自 myfile.py 的样子:

You can then import verifier_c from the verifier package. For example from myfile.py this would look like:

from verifier import verifier_c
...

您可以为每个文件管理一个单独的程序包(和文件夹) Cython扩展或创建一个包含所有子文件夹的子文件夹。您还必须将其他模块传递给 cythonize 。它可以处理 glob模式,glob模式列表或 Distutils.Extensions 对象。后者可以方便地指定cython编译器指令

You can manage a separate package (and folder) for each Cython extension or create one sub folder that contains all of them. You have to pass the other modules to cythonize as well. It can handle a glob pattern, list of glob patterns or a list of Distutils.Extensions objects. The latter can be handy to specify cython compiler directives

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

extensions = [
    Extension("verifier_c", ["verifier/verifier_c.pyx"]),
    Extension("something_else", ["foobar/something_else.pyx"] compiler_directives={'embedsignature': True}),
    ]

setup(
    ext_modules=cythonize(extensions),
)

我希望这个帮助:)

这篇关于当__init__.py存在时,带有设置的Python构建cython扩展会创建子文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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