gpt4 book ai didi

cython - 如何在 Cython 中指定 `.pxd` 文件的路径

转载 作者:行者123 更新时间:2023-12-04 15:18:19 25 4
gpt4 key购买 nike

我的项目具有以下目录结构:

.
├── Makefile
├── pxd
├── pyx
│   ├── Landscaping.pyx
│   ├── Shrubbing.pxd
│   └── Shrubbing.pyx
└── setup.py

但是,如果我移动 Shrubbing.pxd在其他任何地方,比如说,进入 pxd/ ,我收到以下错误:
Error compiling Cython file:
------------------------------------------------------------
...
import pyx.Shrubbing
cimport Shrubbing
^
------------------------------------------------------------

pyx/Landscaping.pyx:2:8: 'Shrubbing.pxd' not found

Error compiling Cython file:
------------------------------------------------------------
...
import pyx.Shrubbing
cimport Shrubbing

cdef Shrubbing.Shrubbery sh
^
------------------------------------------------------------

这很奇怪,因为在 setup.py我有:
from distutils.core import setup, Extension
from Cython.Build import cythonize
setup(ext_modules=cythonize([
Extension(
'pyx.Landscaping',
sources=["pyx/Landscaping.pyx"],
include_dirs=['pxd']), # <-- HERE
Extension('pyx.Shrubbing', sources=["pyx/Shrubbing.pyx"])
]))

明确指定 Shrubbing.pxd 的新目录.

源文件都很短,但为了避免这篇文章困惑,我将发布一个存储库的链接: https://github.com/lobachevzky/landscaping

谢谢你的帮助。

最佳答案

include_dirs用于 C/C++ 头文件,而不是 Cython pxd文件。

一般情况下最好保持相关pyx/pxd文件放在同一个目录下,即 Shrubbing.pyx 和 Shrubbing.pxd 应该在同一个目录下。

然后从其他模块使用它,包括 __init__.pxdcimport通过扩展中使用的名称,例如 pyx.Shrubbing就像使用 Python 模块一样。

如果在 python 中导入(不是 cimport ),__init__.py也应该包括在内。

在同一模块中使用时,OTOH,.pxd需要在该模块的运行时可用,这意味着将其包含在 Python 搜索路径中。

如果要将 pxd 文件组织到单独的目录中,则将它们符号链接(symbolic link)到模块目录中以使它们可用于模块,该目录包含 __init__.pxd.py文件。

这有点困惑,因为 Cython 目前不支持相对导入,因此在想要从另一个目录导入时需要链接。

documentation for more details .

关于cython - 如何在 Cython 中指定 `.pxd` 文件的路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47361418/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com