Skip to main content
 首页 » 编程设计

python之Cythonize python 模块到不同的目标目录

2024年10月24日12txw1958

我正在尝试对一些 python 模块(在 Windows 上)进行 cythonize,并希望将生成的 .c 文件存储在不同的目录中。

我试过这样的:

from Cython.Build import cythonize 
module_list = cythonize(r'C:\myproject\module.py',  
                        force=True, 
                        build_dir=r'C:\myproject\compiled_modules') 

编译已完成,但文件 module.c 是在 C:\myproject 中创建的,而不是按预期在 C:\myproject\compiled_modules 中创建的。我做错了什么?

请您参考如下方法:

我不确定 cythonize 函数是否可以强制执行此操作。我只想使用命令行:

/path/to/cython yourmod.py -o compiled_modules/yourmod.c 

编辑:

可以使用 subprocess 模块从 python 脚本调用此命令:

import subprocess 
subprocess.check_call(["/path/to/cython", "yourmod.py", "-o", "compiled_modules/yourmod.c"]) 

编辑 2:

根据 this Sage developer ,cython 不支持重定位 c/cpp 文件,因此看起来从命令行编译或使用普通配置然后移动 c/cpp 文件是您唯一的选择。