为什么这个 .pyx 文件没有被识别为模块?
Why isn't this .pyx file being recognized as a module?
我在使用相对导入时遇到了问题,但我似乎无法弄清楚这种情况出了什么问题。它似乎 就像是从同一包中的另一个模块直接相对导入,所以我不知道如何调试它。
我的项目是这样设置的:
.
├── ckmeans
│ ├── __init__.py
│ ├── _ckmeans.pxd
│ ├── _ckmeans_wrapper.pyx
│ ├── _ckmeans.py
│ ├── _evaluation.py
│ └── _utils.py
└── setup.py
在__init__.py
的顶部:
from ._ckmeans import ckmeans # _ckmeans.py
并且在 _ckmeans.py
的顶部:
from . import _ckmeans_wrapper # _ckmeans_wrapper.pyx
并且在 _ckmeans_wrapper.pyx
的顶部:
cimport _ckmeans # _ckmeans.pxd
我运行pip install --ignore-installed --upgrade -e .
,一切似乎都很顺利。然后当我尝试 运行 我的测试套件,或 import ckmeans
在解释器中,我得到错误:
ImportError: cannot import name '_ckmeans_wrapper'
当我在解释器中注释掉 __init__.py
和 import ckmeans
的导入语句时,它确实似乎缺少 _ckmeans_wrapper
模块。我怀疑在 Cython 构建中有些东西悄无声息地失败了,但我不知道如何调试。
这是 setup.py
:
import numpy as np
from Cython.Build import cythonize
from setuptools import setup, Extension
extension = Extension(
name='_ckmeans_wrapper',
sources=['ckmeans/_ckmeans_wrapper.pyx'],
language="c++",
include_dirs=[np.get_include()]
)
setup(
name='ckmeans',
version='1.0.0',
packages=['ckmeans'],
ext_modules = cythonize(extension),
install_requires=['numpy', 'Cython']
)
Extension
的 name
参数不正确。应该是 name='ckmeans._ckmeans_wrapper'
.
我在使用相对导入时遇到了问题,但我似乎无法弄清楚这种情况出了什么问题。它似乎 就像是从同一包中的另一个模块直接相对导入,所以我不知道如何调试它。
我的项目是这样设置的:
.
├── ckmeans
│ ├── __init__.py
│ ├── _ckmeans.pxd
│ ├── _ckmeans_wrapper.pyx
│ ├── _ckmeans.py
│ ├── _evaluation.py
│ └── _utils.py
└── setup.py
在__init__.py
的顶部:
from ._ckmeans import ckmeans # _ckmeans.py
并且在 _ckmeans.py
的顶部:
from . import _ckmeans_wrapper # _ckmeans_wrapper.pyx
并且在 _ckmeans_wrapper.pyx
的顶部:
cimport _ckmeans # _ckmeans.pxd
我运行pip install --ignore-installed --upgrade -e .
,一切似乎都很顺利。然后当我尝试 运行 我的测试套件,或 import ckmeans
在解释器中,我得到错误:
ImportError: cannot import name '_ckmeans_wrapper'
当我在解释器中注释掉 __init__.py
和 import ckmeans
的导入语句时,它确实似乎缺少 _ckmeans_wrapper
模块。我怀疑在 Cython 构建中有些东西悄无声息地失败了,但我不知道如何调试。
这是 setup.py
:
import numpy as np
from Cython.Build import cythonize
from setuptools import setup, Extension
extension = Extension(
name='_ckmeans_wrapper',
sources=['ckmeans/_ckmeans_wrapper.pyx'],
language="c++",
include_dirs=[np.get_include()]
)
setup(
name='ckmeans',
version='1.0.0',
packages=['ckmeans'],
ext_modules = cythonize(extension),
install_requires=['numpy', 'Cython']
)
Extension
的 name
参数不正确。应该是 name='ckmeans._ckmeans_wrapper'
.