2

我有以下目录结构:

testcython/
    setup.py
    testcython/
        __init__.py
        foo.pyx
        stuff.py
        bar/
            __init__.pxd
            __init__.py
            bar.pxd
            bar.pyx

其中文件内容如下:

bar.pxd

# cython: language_level=3

cdef int square(int x)

bar.pyx

# cython: language_level=3

cdef int square(int x):
    return x * x

foo.pyx

# cython: language_level=3

import cython
cimport numpy as np
import numpy as np

from .Bar cimport square

def do_square(x):
    return square(x)

stuff.py

from __future__ import print_function

from .Foo import do_square

def do():
    print(do_square(2))

setup.py

import os, sys

from Cython.Build import build_ext, cythonize
from setuptools import setup, Extension, find_packages

def ext_modules():
    import numpy as np

    include_dirs = ['.', np.get_include()]
    root_dir = os.path.abspath(os.path.dirname(__file__))
    bar_ext = Extension(
        "Bar",
        sources=[root_dir + "/testcython/bar/bar.pyx"],
        include_dirs=include_dirs,
    )
    foo_ext = Extension(
        "Foo",
        sources=[root_dir + "/testcython/foo.pyx"],
        include_dirs=include_dirs
    )
    exts = [bar_ext, foo_ext]

    return cythonize(exts)

REQUIREMENTS = [
    "numpy",
    "cython"
]

setup(
    name="testcython",
    packages=find_packages(),
    ext_package="testcython",
    ext_modules=ext_modules(),
    cmdclass={"build_ext" : build_ext},
    zip_safe=False,
    install_requires=REQUIREMENTS
)

问题

问题是,当我尝试安装它(pip install -e .在顶级testcython目录中)时,我从 Cython 收到以下错误:

Complete output from command python setup.py egg_info:

    Error compiling Cython file:
    ------------------------------------------------------------
    ...

    import cython
    cimport numpy as np
    import numpy as np

    from .Bar cimport square
    ^
    ------------------------------------------------------------

    testcython/foo.pyx:7:0: relative cimport beyond main package is not allowed

    Error compiling Cython file:
    ------------------------------------------------------------
    ...
    import numpy as np

    from .Bar cimport square

    def do_square(x):
        return square(x)
              ^
    ------------------------------------------------------------

这个答案(cython: relative cimport beyond main package is not allowed)意味着在对象的参数中包含根目录( '.')应该可以解决问题。include_dirsExtension

虽然Cython 文档的这一部分提到在使用包时zip_safe=False的参数中使用。setupsetuptools

正如您从我setup.py上面的文件中看到的那样,我已经包含了这两个 - 但我仍然收到上面的错误。

注意:如果我将扩展名(在Extension构造函数中)分别从BarFoo更改为testcython.Bartestcython.Foo,那么我会得到一个不同的错误:

Complete output from command python setup.py egg_info:

    Error compiling Cython file:
    ------------------------------------------------------------
    ...

    import cython
    cimport numpy as np
    import numpy as np

    from .Bar cimport square
    ^
    ------------------------------------------------------------

    testcython/foo.pyx:7:0: 'testcython/Bar/square.pxd' not found

    Error compiling Cython file:
    ------------------------------------------------------------
    ...
    import numpy as np

    from .Bar cimport square

    def do_square(x):
        return square(x)
              ^
    ------------------------------------------------------------
4

1 回答 1

3

我在同事的帮助下解决了这个问题,所以我会在这里提到解决方案,以防将来对人们有所帮助。

问题与 Cython 模块的导入方式有关,更具体地说,与.so构建扩展时文件的放置位置有关。最初,该Bar.so文件是在testcython目录中生成的 - 因此在尝试从bar子模块导入时,它找不到相应的共享对象文件。

为了解决这个问题,我需要"bar.bar"在创建此扩展时使用该名称,这会导致.so文件被生成到testcython/bar目录中。然后,在 中foo.pyx,要使用此bar模块中的成员,必须将导入更改为from testcython.bar.bar cimport <name>.

笔记:

此外,square问题中显示的函数不能以这种形式从另一个 Cython 模块使用,因为没有__pyx_capi__为免费cdef函数生成。相反,这个函数必须cdef作为静态方法包装在某个类中,以便从另一个 Cython 模块中使用它,即:

cdef class Square:
    @staticmethod
    cdef int square(int x)

然后可以导入,foo.pyx例如,使用from testcython.bar.bar cimport Square. 然后,该类Square本质上就像一个“命名空间”。

于 2019-06-28T12:48:46.747 回答