首页 > 解决方案 > 使用 Cython 编译多 python 文件并生成一个 so

问题描述

我有一些 python 文件,并且所有操作都在 Main 文件中暴露给其他人。我想编译它们并分发给.so其他人。

我尝试用 cython 编译它们,但生成了不止一个.so(有效但不完美)。

例子:

文件夹结构为:

CythonExample/
|——for_call.py
|——setup.py
|——Student.py

安装程序.py

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

setup(
    ext_modules=cythonize(["Student.py", "for_call.py"]),
)

python setup.py build_ext --inplace然后我得到Student.sofor_call.so

我将 setup.py 修改为:

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

setup(
    name='sayname',
    ext_modules=cythonize(Extension("sayname", sources=["Student.py", "for_call.py"], language="c")),
)

然后 python setup.py build_ext --inplace,好吧,只sayname.so生成。但是当我尝试导入它时。

import sayname

ImportError: dynamic module does not define init function (initsayname)

谢谢。

标签: pythoncython

解决方案


推荐阅读