【发布时间】: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__.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']
)
【问题讨论】:
标签: python-3.x cython python-import setuptools