本文介绍了用“ python compile.py”编译cython代码。并且没有“构建”命令行参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个compile.py脚本:

I have a compile.py script:

from distutils.core import setup
from Cython.Build import cythonize
setup(ext_modules = cythonize("module1.pyx"))

Cython代码。缺点是我必须使用命令行参数 build 来调用它:

that compiles my Cython code. The drawback is that I have to call it with a command-line parameter build:

python compile.py build

相反,我希望能够将此 compile.py ,并使用 + 。为此,它应该在以下位置工作:

Instead, I would like to be able to call this compile.py directly from Sublime Text, as usual, with +. To do that, it should work from:

python compile.py

问题:如何修改上述脚本,以便可以使用 python compile.py ?

Question: how to modify the above script so that it can be run with python compile.py?

推荐答案


  • 方法1 >:

    使用 script_args 就像这样:

    setup(ext_modules=cythonize("module1.pyx", build_dir="build"), script_args=['build'])
    

    setup(ext_modules=cythonize("module1.pyx", build_dir="build"), script_args=['build_ext'])
    

    (两者均有效)

    如果希望输出文件位于同一目录中,则可以使用:

    If you want the output files to be in the same directory, you can use:

    setup(ext_modules=cythonize("module1.pyx", build_dir="build"), script_args=['build'], 
                                                options={'build':{'build_lib':'.'}})
    

    setup(ext_modules=cythonize("module1.pyx", build_dir="build"), script_args=['build_ext'],
                                                options={'build_ext':{'inplace':True}})
    


  • 方法#2

    在顶部添加此内容:

     import sys; sys.argv = ["", "build"]
    

    有点破译,但可以很好,并且避免创建新的构建系统,例如使用(链接由@Melvin提供)。

    It's a bit hack-ish but it works fine, and avoids to have to create a new build-system, like with Build and run with arguments in Sublime Text 2 (link kindly provided by @Melvin).

    这篇关于用“ python compile.py”编译cython代码。并且没有“构建”命令行参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 09:09