Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Customize location of .so file generated by Cython

I have a Cython package with wrappers of a C library. This is the tree structure of the package

package/
       _api.pxd
       _wrap.pyx
       setup.py
       wrapper/
              __init__.py
              wrap.py

Doing

python setup.py build_ext --inplace

puts the _wrap.so file in the top-level package/ directory which is normally required in most cases. However, my wrap.py needs the _wrap.so in the package/wrapper/ directory. I was wondering if there's a way in which setup.py could create the .so file in the desired place by itself without manually copying and pasting it in the location.

like image 588
Himanshu Mishra Avatar asked Jun 25 '15 07:06

Himanshu Mishra


People also ask

Does Cython need to be compiled?

Cython source file names consist of the name of the module followed by a . pyx extension, for example a module called primes would have a source file named primes. pyx . Cython code, unlike Python, must be compiled.

What is Cimport?

c file). The cimport statement is used by Cython to import definitions from a . pxd file. This is different than using a normal Python import statement, which would load a regular Python module.


1 Answers

The output folder for the produced .so files can be specified as the first argument of setuptools.Extension function.

Here is an example for Cython extensions,

from setuptools import setup, find_packages, Extension
from Cython.Distutils import build_ext

ext_modules=[
    Extension("package.wrapper.wrap",    # location of the resulting .so
             ["package/wrapper/wrap.pyx"],) ]


setup(name='package',
      packages=find_packages(),
      cmdclass = {'build_ext': build_ext},
      ext_modules = ext_modules,
     )
like image 123
rth Avatar answered Oct 24 '22 18:10

rth