kiriloff
kiriloff

Reputation: 26333

How do I use Cython for external module compilation, from within my setup.py file?

First, if i have a project that is all python (no C++ or C) files, what is the advantage of using Cython, vs, say, using the python library directly (for integration in a large C++ software program) ?

Second, to compile the .pyx files to Cython, i intend to use setup.py file with this code:

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

setup(
    ext_modules = cythonize(MyString)
)

What should MyString be ? the name of the python (.pyx) files without extension ? should it include a full path ? where should be located the setup.py file so that it cythonizes the files ? If not this way, how to properly Cythonize the files ?

Upvotes: 2

Views: 1253

Answers (2)

Jim Dennis
Jim Dennis

Reputation: 17520

might give you better performance gains more quickly.

Upvotes: 0

hivert
hivert

Reputation: 10667

Hum there is a lot of question here.

  1. what is the advantage of using Cython, vs, say, using the python library directly ?

    If you only use Python library (eg: for network stuff), using Cython should not be of great help. Cython will greatly help you if:

    • you need access to external C/C++ library;
    • or you need to build your own fast data structure possibly using C/C++ allocation;
    • or you have an intensive number crunching application.
  2. What should MyString be ?

    They are a lot of option here. I usually use

    setup(
        cmdclass = {'build_ext': build_ext},
        ext_modules = [Extension("myext", ["myext.pyx", "myextlib.pyx"])])
    

    You'll find a lot of information at Cython compilation documentation For a small library, you can put it in the same directory as the source.

Upvotes: 1

Related Questions