You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

加载Cython生成的.so文件后调用类方法报错:找不到pre_process属性

问题原因与解决方案

核心问题

你犯了两个关键错误:

  1. 混淆了类方法与模块函数的调用方式:pre_process、transform是EnterQueryFastText类的实例方法,不是模块的直接顶层函数,不能直接通过your_module.pre_process调用。
  2. 未确认Cython类是否被正确导出到模块:如果编译配置或代码细节有问题,模块可能无法正确暴露EnterQueryFastText类。

分步解决

1. 修正调用逻辑

先从加载的模块中获取EnterQueryFastText类,实例化后再调用类方法:

# 保留原有模块加载代码
loader = ExtensionFileLoader("EnterQueryFastText", "../pipelines/EnterQueryFastText/EnterQueryFastText.cpython-310-darwin.so")
spec = importlib.util.spec_from_file_location("EnterQueryFastText", location="../pipelines/EnterQueryFastText/EnterQueryFastText.cpython-310-darwin.so", loader=loader)
your_module = importlib.util.module_from_spec(spec)
sys.modules["EnterQueryFastText"] = your_module
spec.loader.exec_module(your_module)

# 修正后的方法调用逻辑
input = {"query":"achi"}
# 先实例化Cython类
model_instance = your_module.EnterQueryFastText()
# 通过类实例调用方法
pre_proc_output = model_instance.pre_process(input)
transform_output = model_instance.transform(pre_proc_output)
post_proc_output = model_instance.post_process(transform_output)
print(post_proc_output)

2. 验证Cython代码的导出正确性

确保你的Cython代码(.pyx文件)满足以下要求:

  • cdef class EnterQueryFastText必须处于模块顶层作用域,不能被嵌套在内部函数或其他作用域中。
  • 类需要有可被Python调用的构造方法(__init__),且方法类型为def或cpdef:
    cdef class EnterQueryFastText:
        def __init__(self):
            # 在此初始化模型资源,比如加载FastText预训练模型
            self.model = self._load_fasttext_model()
    
        cpdef transform(self, payload):
            cdef embedding = self._get_sentence_vector(payload)
            return embedding
    
        cpdef pre_process(self, input: dict):
            cdef pre_processed_input = cleanText(input['query'])
            return pre_processed_input
    
        # 私有方法用cdef定义不影响外部调用
        cdef _get_sentence_vector(self, payload):
            # 实现向量生成逻辑
            pass
    
        cdef _load_fasttext_model(self):
            # 实现模型加载逻辑
            pass
    

3. 检查编译配置(setup.py)

确保setup.py正确配置Cython扩展,将类打包到目标模块中:

from setuptools import setup, Extension
from Cython.Build import cythonize

extensions = [
    Extension(
        "EnterQueryFastText",
        ["EnterQueryFastText.pyx"],
        # 根据环境添加编译优化参数,比如链接FastText依赖库
        extra_compile_args=["-O3"],
    )
]

setup(
    name="EnterQueryFastText",
    ext_modules=cythonize(extensions),
)

额外验证步骤

先尝试常规导入方式测试模块是否正常,排除动态加载的干扰:

import sys
sys.path.append("../pipelines/EnterQueryFastText")
import EnterQueryFastText

model = EnterQueryFastText.EnterQueryFastText()
print(model.pre_process({"query":"achi"}))

如果常规导入也报错,说明编译后的模块本身存在问题,优先排查Cython代码逻辑和编译配置;如果常规导入正常,再检查动态加载的路径或命名是否存在拼写错误。

内容的提问来源于stack exchange,提问作者coderAsk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 01:17:44