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

Cython编译生成的模块导入时提示ModuleNotFoundError问题求助

Cython模块编译及导入失败解决方案

1 修正setup.py配置错误

当前setup.py中cythonize参数指定的文件名与实际待编译的sum.pyx不匹配,同时需要补充language_level配置消除版本警告,修改后代码如下:

from distutils.core import setup
from Cython.Build import cythonize

setup(ext_modules = cythonize('sum.pyx', compiler_directives={'language_level' : "3"}))

2 解决Unable to find vcvarsall.bat编译错误

该错误是因为Python默认会查找MSVC编译工具链,你使用的是MSYS2的g++编译器,需要手动指定编译器类型,两种操作方式可选:

  • 单次编译指定参数:执行编译命令时添加--compiler=mingw32,完整命令如下:
    python setup.py build_ext --inplace --compiler=mingw32
  • 永久配置默认编译器:在Python安装路径的Lib\distutils目录下新建distutils.cfg文件,写入以下配置:
[build]
compiler=mingw32
[build_ext]
compiler=mingw32

注意:需提前将MSYS2安装目录下的bin文件夹路径加入系统环境变量PATH,确保终端可以正常调用gcc、g++命令。

3 功能验证

编译成功后,当前目录下会生成后缀为.pyd的Python扩展模块(Windows平台对应Linux的.so文件),此时运行test.py即可正常导入sum模块,输出计算结果11。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.28 23:45:04