简体   繁体   English

Cython:控制cythonize以停止将来自.pyx文件的已编译.c文件放置在安装目录中

[英]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. 我有一个包含敏感代码的小型辅助函数test To mask this code, I have written the function in hello.pyx and using it in my package mypackage . 为了屏蔽此代码,我已经在hello.pyx编写了该函数,并在我的包mypackage使用了该函数。

I am able to build and use it by modifying the setup.py for the package to something like below: 我可以通过将包的setup.py修改为以下内容来构建和使用它:

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/ ) 但是,当我通过python setup.pypip install构建/安装它时,cython生成的hello.chello.so都被放置在安装目录中(在我的情况下是~/.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. 我可以从安装目录中手动删除hello.c (仅hello.so文件),并且该程序包运行良好。

Is there a way I can automate this process so that i don't need to manually remove the compiled c file ? 有没有一种方法可以使此过程自动化,从而无需手动删除已编译的c文件?

I looked at this stackoverflow question . 我看着这个stackoverflow问题 However, I am getting an error during cythonize operation when i am trying to build the wheel using pip wheel . 但是,当我尝试使用pip wheel .构造轮子时,在cythonize操作期间出现错误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 另外,就我而言,只要安装的代码不包含hello.c纯文本文件,我可以使用tar.gz进行安装

[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 我可以通过在setup.py使用include_package_data=False来停止在安装目录中放置.c文件。但是,不确定是否此选项是否意味着项目中的非python文件

How do you intend to distribute the package? 您打算如何分发包裹?

WIthout the .c files in the source distributable, Cython becomes a dependency for your project. 如果没有源分发中的.c文件,Cython将成为您项目的依赖项。 As Cython is a static code generator, it is not required after the .c files are generated. 由于Cython是静态代码生成器,因此生成.c文件之后就不需要它了。

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. 通过将它们包括在您的源代码发行版中,可以通过在.c文件上使用标准setuptools扩展名而不是在.pyx文件上使用cythonize的方式,安装软件包而无需安装.pyx

This is also the recommendation in Cython documentation which shows how to switch between the two for development and distributing. 这也是Cython文档中的建议, 该文档显示了如何在两者之间进行切换以进行开发和分发。

If you really want to exclude them and make Cython a dependency, add the following to MANIFEST.in 如果您确实要排除它们并使Cython成为依赖项,请将以下内容添加到MANIFEST.in

exclude mypackage/*.c

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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