简体   繁体   English

Python setup.py调用makefile不包含二进制文件

[英]Python setup.py call makefile don't include binaries

Some context: I have some C code that when compiled I can call in the terminal like this: ./my_excec -params It generates some files that I am using in python to generate charts, and other stuff. 一些上下文:我有一些C代码,在编译时我可以在终端中调用如下: ./my_excec -params它生成一些我在python中使用的文件来生成图表和其他东西。

I want to pack everything in a python library both the C code and the python code. 我想在python库中打包C代码和python代码。 The C code is not a python extension (prolly going to be in a future but right now is not). C代码不是python扩展(将来会很多,但现在不是)。

I have a make file to compile the C code and I know I can call it from the setup.py like this: subprocess.call(['make', '-C', 'word2vec-src']) 我有一个make文件来编译C代码,我知道我可以从setup.py这样调用它: subprocess.call(['make', '-C', 'word2vec-src'])

What I want to be able to do is: pip install my_module That should call the makefile, compile the C so the user can call the binaries: my_excec -params and also be able to import the python code around it. 我希望能够做的是: pip install my_module应该调用makefile,编译C,这样用户就可以调用二进制文件: my_excec -params并且还能够导入它周围的python代码。

The problem I am having is when packaging the python package. 我遇到的问题是打包python包时。 I am using the data_files option in setup() like this: data_files=[('bin', ['bin/binary_file'])], This moves the files from bin to the installation folder (in a virtual env) and I can call them. 我在setup()使用data_files选项,如下所示: data_files=[('bin', ['bin/binary_file'])],这将文件从bin移动到安装文件夹(在虚拟环境中),我可以给他们打电话。 But when packaging is also putting the compiled files in the tarball and when I call pip install my_module` is putting the compiled files from my computer. 但是,当打包时也将编译后的文件放在tarball中,当我调用pip install时,my_module`正在从我的计算机中放入已编译的文件。

Thanks. 谢谢。

I was able to find a really easy solution. 我找到了一个非常简单的解决方案。

As I said my main problem was that I was packaging the compiled files. 正如我所说,我的主要问题是我正在打包已编译的文件。 To exclude those files form the tarball/zip just had to put this on MANIFEST.in: prune bin . 要从tarball / zip中排除这些文件,只需将它放在MANIFEST.in:prune prune bin

Then just need to call the makefile from setup.py: 然后只需要从setup.py调用makefile:

directory = 'bin'
if not os.path.exists(directory):
    os.makedirs(directory)

subprocess.call(['make', '-C', 'src'])

With that when someone does pip install whatever is going to call the make file and put the binaries on bin (have to specify this on the make file). 有了这个,当有人做pip install whatever将调用make文件并将二进制文件放在bin (必须在make文件中指定它)。

Then just need to say the setup to copy those files: 然后只需要说设置来复制这些文件:

setup(
...
data_files=[('bin', ['bin/binaries'])],
)

Done! 完成! Hopefuly someone find this usefull :) Hopefuly有人觉得这很有用:)

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

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