Cython: control cythonize to stop placing compiled `.c` file from `.pyx` file in install directory
Asked Answered
R

1

5

I have a small helper function test containing sensitive code. To mask this code, I have written the function in hello.pyx and using it in my package mypackage.

I am able to build and use it by modifying the setup.py for the package to something like below:

import os                                                                                                                                             
from setuptools import setup, find_packages                                                                                                           
from Cython.Build import cythonize                                                                                                                    
os.chdir(os.path.normpath(os.path.join(os.path.abspath(__file__), os.pardir)))                                                                        

setup(                                                                                                                                                
    name='mypackage',                                                                                                                                  
    ext_modules = cythonize('mypackage/hello.pyx'),                                                                                             
    packages=find_packages(),                                                                                                                         
    include_package_data=True,                                                                                                                        
    )

However, when i build/install it via python setup.py or pip install, the cython generated hello.c as well as hello.so are getting placed in the install directory (in my case ~/.local/python2.7/site-packages/mypackage/)

I can manually remove the hello.c from the install directory (leaving just the hello.so file) and the package runs fine.

Is there a way I can automate this process so that i don't need to manually remove the compiled c file ?

I looked at this stackoverflow question. However, I am getting an error during cythonize operation when i am trying to build the wheel using pip wheel . . Also, in my case I am fine with installing using tar.gz as long as the installed code doesn't contain plain text files for hello.c

[Edit]

I was able to stop placing a .c file in the install directory by using include_package_data=False in my setup.py.. However, am not exactly sure whether this option means for non python files in the project

Recidivate answered 25/7, 2017 at 22:0 Comment(2)
Do you have MANIFEST.in in your project directory? If yes, what does it contain?Sapor
There is no MANIFEST.in.. btw just updated the questionRecidivate
P
7

How do you intend to distribute the package?

WIthout the .c files in the source distributable, Cython becomes a dependency for your project. As Cython is a static code generator, it is not required after the .c files are generated.

By including them in your source distribution, the package can be installed without needing to have Cython by using standard setuptools extensions on the .c files rather than using cythonize on the .pyx files.

This is also the recommendation in Cython documentation which shows how to switch between the two for development and distributing.

If you really want to exclude them and make Cython a dependency, add the following to MANIFEST.in

exclude mypackage/*.c

Photomural answered 7/8, 2017 at 14:36 Comment(1)
I am ok with Cython being a dependency. Thanks this worksRecidivate

© 2022 - 2024 — McMap. All rights reserved.