pythonpipcythonsetuptoolscythonize

Edit location of compiled files with setuptools


I am using setuptools to compile a pyx file using Cython using the following code in my setup.py

from Cython.Distutils import build_ext

extensions=[Extension("filtering.filter", "filtering/filter.pyx")

setup(
   name="..",
   ........
   ext_modules=extensions,
   cmdclass={"build_ext", build_ext}
   include_dirs=[".", numpy.get_include()]
)

I want to use pip install . to install this rather than python setup.py ... When running pip install . it compiles the file correctly, but stores it in the wrong place, it stores it in filtering/ rather than my_project/filtering/

I have tried using a setup.cfg with inplace=1 and also tried build_lib=. but this does not put it in the correct place either Any help appreciated


Solution

  • I'm writing a package in which all the code goes inside the src directory, and this approach works fine for me.

    Package structure

    src
     |-- foo
     |   |-- foo.pyx
     |
     |-- setup.py
     |-- setup.cfg
     |-- (.toml, LICENSE ... )
    

    setup.cfg

    [metadata]
    .
    .
    .
    [options]
    packages = find:
    package_dir = src
    .
    .
    .
    [options.packages.find]
    where = src
    

    setup.py

    from setuptools import setup
    from setuptools.extension import Extension
    from Cython.Build import cythonize
    
    ext_modules = [
        Extension(
            name = "foo.foo.pyx",
            sources = ["src/foo/foo.pyx"]
        )
    ]
    
    if __name__ == "__main__":
    
        setup(
            ext_modules=cythonize(ext_modules)
        )
    

    I'm not sure if the last part of the .cfg file (where ...) is strictly necessary.