导入HistomicsTK遇ModuleNotFoundError,疑与Cython相关求解决
解决HistomicsTK导入时的Cython相关ModuleNotFoundError错误
问题重现
在Windows系统按照官方说明安装HistomicsTK(一款数字病理图像分析Python包)后,执行导入代码时触发ModuleNotFoundError,提示找不到histomicstk.segmentation.label._trace_object_boundaries_cython模块。
导入代码:
import histomicstk as htk import numpy as np import scipy as sp import skimage.io import skimage.measure import skimage.color import matplotlib.pyplot as plt import matplotlib.patches as mpatches %matplotlib inline #Some nice default configuration for plots plt.rcParams['figure.figsize'] = 10, 10 plt.rcParams['image.cmap'] = 'gray' titlesize = 24
错误回溯:
ModuleNotFoundError Traceback (most recent call last) Cell In[1], line 1 ----> 1 import histomicstk as htk 3 import numpy as np 4 import scipy as sp File \HistomicsTK\histomicstk\__init__.py:2 1 # import sub-packages to support nested calls ----> 2 from . import segmentation # must be imported before features 3 from . import utils # must be imported before other packages 4 from . import (annotations_and_masks, features, filters, preprocessing, 5 saliency, workflows) File \HistomicsTK\histomicstk\segmentation\__init__.py:11 8 from histomicstk.utils.simple_mask import simple_mask 10 # import sub-packages to support nested calls ---> 11 from . import label, level_set, nuclear, positive_pixel_count 12 from .embed_boundaries import embed_boundaries 13 from .rag import rag File \HistomicsTK\histomicstk\segmentation\label\__init__.py:18 16 from .shuffle import shuffle 17 from .split import split ---> 18 from .trace_object_boundaries import trace_object_boundaries 19 from .width_open import width_open 21 # list out things that are available for public use File \HistomicsTK\histomicstk\segmentation\label\trace_object_boundaries.py:3 1 import numpy as np ----> 3 from ._trace_object_boundaries_cython import _trace_object_boundaries_cython 6 def trace_object_boundaries(im_label, 7 conn=4, trace_all=False, 8 x_start=None, y_start=None, 9 max_length=None, 10 simplify_colinear_spurs=True, 11 eps_colinear_area=0.01): 12 """Performs exterior boundary tracing of one or more objects in a label 13 mask. If a starting point is not provided then a raster scan will be performed 14 to identify the starting pixel. (...) 63 64 """ ModuleNotFoundError: No module named 'histomicstk.segmentation.label._trace_object_boundaries_cython'
解决方案
这个错误的核心是Cython扩展模块未被正确编译或安装,以下是具体修复步骤:
安装Cython依赖
先确保系统中已安装Cython,执行命令:pip install cython从源码重新编译安装HistomicsTK
预编译的HistomicsTK包在Windows环境可能存在兼容性问题,建议直接从源码编译:- 克隆项目源码(若已下载可跳过此步):
git clone https://github.com/DigitalSlideArchive/HistomicsTK.git - 进入项目目录:
cd HistomicsTK - 以开发模式安装并编译Cython扩展:
此命令会自动编译所有Cython模块,并将包以可编辑模式安装到当前Python环境。pip install -e .
- 克隆项目源码(若已下载可跳过此步):
确认Python环境一致性
确保安装和运行代码使用的是同一个Python环境,避免conda与pip混用导致路径冲突。可通过以下命令查看当前Python路径:where python # Windows系统安装时使用对应路径的pip,例如:
python -m pip install -e .验证编译结果
进入histomicstk/segmentation/label/目录,检查是否生成了类似_trace_object_boundaries_cython.cp310-win_amd64.pyd的文件(cp310对应Python3.10版本,版本号会随你的Python版本变化),若存在则说明Cython模块编译成功。
内容的提问来源于stack exchange,提问作者hailey
相关产品推荐
相关产品推荐

