简体   繁体   English

在setup.py中的setup()之前点安装软件包

[英]Pip install package prior to setup() in setup.py

I have some .proto gRPC files I want to compile as part of the setup.py script. 我有一些要编译为setup.py脚本一部分的.proto gRPC文件。 This requires running from grpc_tools import protoc and calling protoc before setup(args) . 这需要运行from grpc_tools import protoc并呼吁protoc setup(args) The goal is to compile and install the pb files from pip install pkgname . 目的是从pip install pkgname编译并安装pb文件。

Eg 例如

# setup.py

# generate our pb2 files in the temp directory structure
compile_protobufs(pkgname)

# this will package the generated files and put them in site-packages or .whl
setup(
    name=pkgname,
    install_requires=['grpcio-tools', ...],
    ...
)

This works as intended, I get the pb files in my site-packages or in the wheel without them having to exist in the source folder. 这可以按预期工作,我可以将pb文件放在网站包中,也可以放在轮子中,而不必将它们存在于源文件夹中。 However, this pattern means I cannot naively pip install pkgname from scratch, as the step compile_protobufs depends on grpcio-tools , which does not get installed until setup() . 但是,这种模式意味着我不能天真地从头开始grpcio-tools pip install pkgname ,因为步骤compile_protobufs依赖于grpcio-tools ,直到setup()setup()

I could use setup_requires, but that is on the chopping block . 我可以使用setup_requires,但这是在砧板上 I could just install the dependencies first (right now I use RUN pip install -r build-require.txt && pip install pkgname/ ), but it still seems like there ought to be a cleaner way. 我可以先安装依赖项(现在我使用RUN pip install -r build-require.txt && pip install pkgname/ ),但是似乎仍然应该有一种更RUN pip install -r build-require.txt && pip install pkgname/方法。

Am I even going about this pattern correctly or am I missing some packaging idiom? 我什至正确地采用了这种模式还是缺少一些包装习惯?

My criteria: 我的标准:

  • Generally this is run inside a container, so minimizing external deps 通常,它在容器内运行,因此最大程度地减少了外部消耗
  • I want the _pb2.py files regenerated each time I pip install 我希望每次pip install重新生成_pb2.py文件
  • These files need to also make their way into any .whl or tar. 这些文件还需要进入任何.whl或tar文件。

Looks like it is already documented here: 看起来它已经在这里记录了:

https://github.com/grpc/grpc/tree/master/tools/distrib/python/grpcio_tools#usage https://github.com/grpc/grpc/tree/master/tools/distrib/python/grpcio_tools#usage

So your setup.py could look like this: 因此,您的setup.py可能如下所示:

#!/usr/bin/env python3

import distutils.command.install
import setuptools

class build_package_protos(setuptools.Command):
    user_options = []
    def initialize_options(self):
        pass
    def finalize_options(self):
        pass
    def run(self):
        from grpc_tools import command
        command.build_package_protos(self.distribution.package_dir[''])

class install(distutils.command.install.install):
    _sub_command = ('build_package_protos', None,)
    _sub_commands = distutils.command.install.install.sub_commands
    sub_commands = [_sub_command] + _sub_commands

def setup():
    setuptools.setup(
        # see 'setup.cfg'
        cmdclass={
            'build_package_protos': build_package_protos,
            'install': install,
        },
        setup_requires=[
            'grpcio-tools',
        ],
    )

if __name__ == '__main__':
    setup()

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

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