gpt4 book ai didi

python - 构建包含多个 Cython 扩展的 Python 包

转载 作者:行者123 更新时间:2023-12-02 10:53:26 25 4
gpt4 key购买 nike

我有以下目录结构:

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_dirs ) Extension 的参数对象应该可以解决问题。

this part of the Cython documentation提及使用 zip_safe=Falsesetup 的参数中使用 setuptools 时包裹。

从我的 setup.py 中可以看出上面的文件,我已经包含了这两个 - 但我仍然收到上面的错误。

注:如果我从 Extension 更改扩展名(在 Bar 构造函数中)和 Footestcython.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)
^
------------------------------------------------------------

最佳答案

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

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

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

笔记:

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

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

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

关于python - 构建包含多个 Cython 扩展的 Python 包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56560007/

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