1

I have a compile.py script:

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

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

python compile.py build

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

python compile.py

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

1

1 Answer 1

1
  • Method #1:

    Use script_args like this:

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

    or

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

    (both work).

    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':'.'}})
    

    or

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

    Add this on top:

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

    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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.