简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM