如何在Cython中指定`.pxd`文件的路径 [英] How to specify path to `.pxd` file in Cython

查看:286
本文介绍了如何在Cython中指定`.pxd`文件的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My project has the following directory structure:

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

但是,如果我将 Shrubbing.pxd 移到其他地方,例如,移至 pxd / 时,出现以下错误:

However, if I move Shrubbing.pxd anywhere else, say, into pxd/, I get the following error:

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 中,我有:

This is strange because in setup.py I have:

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 /环境美化

The source files are all very short, but to avoid cluttering this post, I will just post a link to a repository: https://github.com/lobachevzky/landscaping

感谢您的帮助。

推荐答案

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

include_dirs is for C/C++ headers, not Cython pxd files.

通常最好保持相关的 pyx / pxd 文件一起放在同一目录中,即Shrubbing.pyx和Shrubbing.pxd应该在同一目录中。

In general it is best to keep related pyx/pxd files together in same directory, ie Shrubbing.pyx and Shrubbing.pxd should be in same directory.

然后从其他模块中使用该文件,通过扩展名中使用的名称包括 __ init __。pxd cimport ,例如 pyx.Shrubbing 就像使用Python模块一样。

To then use that from other modules, include a __init__.pxd and cimport via the name used in the Extension, eg pyx.Shrubbing as you would with Python modules.

如果使用python导入(不是 cimport ),则 __ init __。py

If importing in python (not cimport), __init__.py should be included as well.

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

When using in the same module, OTOH, the .pxd needs to be available at runtime of that module, which means including it in Python search path.

如果要将pxd文件组织到单独的文件中然后目录将它们象征性地链接到模块目录中,以使它们可用于模块,该目录包含 __ init __。pxd .py 文件。

If you want to organise the pxd files into separate dirs then link them symbolically within the module directory to make them available to the module, the dir containing an __init__.pxd or .py file.

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

It's a bit messy as Cython does not currently support relative imports, hence the need for linking when wanting to import from another dir.

请参见文档以获取更多详细信息

这篇关于如何在Cython中指定`.pxd`文件的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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