如何使用 cythonize 启用 `--embed`?

How to enable `--embed` with cythonize?

在命令行调用 cython 时,可以告诉它创建一个嵌入 Python 解释器的 int main() 方法:

$ cython --embed main.pyx
$ grep 'int main' main.c
int main(int argc, char** argv) {

但是,当您直接 import Cython 时,例如在 distutils setup.py 脚本中,embed 选项似乎被忽略了:

$ python3
>>> from Cython.Compiler import Options
>>> Options.embed = True
>>> from Cython.Build import cythonize
>>> cythonize('main.pyx')
[1/1] Cythonizing main.pyx
>>>
$ grep 'int main' main.c
$

我哪里做错了?

我从 Cython 的资源中弄明白了。

看起来 Cython 需要 Options.embed 的特定值:

Options.embed = "main"