【问题标题】:Why isn't this .pyx file being recognized as a module?为什么这个 .pyx 文件没有被识别为模块?
【发布时间】:2016-06-27 00:56:32
【问题描述】:

我在使用相对导入时遇到了问题,但我似乎无法弄清楚在这种情况下出了什么问题。它似乎就像从同一个包中的另一个模块直接相对导入,所以我不知道如何调试它。

我的项目是这样设置的:

.
├── 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__.pyimport 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']
)

【问题讨论】:

    标签: python-3.x cython python-import setuptools


    【解决方案1】:

    Extensionname 参数不正确。应该是name='ckmeans._ckmeans_wrapper'

    【讨论】:

    • 可以将您自己的答案标记为问题的答案,然后在问题列表中不会显示为未回答。
    猜你喜欢
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    • 2021-02-22
    • 2019-12-12
    • 1970-01-01
    • 2021-01-09
    • 1970-01-01
    • 2013-08-18
    相关资源
    最近更新 更多