在 setup.py 中的 setup() 之前安装包
Pip install package prior to setup() in setup.py
我有一些 .proto
gRPC 文件,我想将其编译为 setup.py 脚本的一部分。这需要运行宁from grpc_tools import protoc
并在setup(args)
之前调用protoc
。目标是编译和安装来自 pip install pkgname
的 pb 文件。
例如
# 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', ...],
...
)
这按预期工作,我在我的站点包或 wheel 中获取了 pb 文件,而无需将它们存在于源文件夹中。但是,这种模式意味着我不能天真地 pip install pkgname
从头开始,因为步骤 compile_protobufs
取决于 grpcio-tools
,直到 setup()
才安装。
我可以使用 setup_requires,但是 that is on the chopping block。我可以先安装依赖项(现在我使用 RUN pip install -r build-require.txt && pip install pkgname/
),但似乎仍然应该有一种更简洁的方法。
我是否正确地使用了这个模式,或者我是否遗漏了一些包装惯用语?
我的标准:
- 通常这是 运行 在容器内,因此尽量减少外部依赖
- 我希望每次
pip install
时重新生成 _pb2.py
文件
- 这些文件还需要进入任何
.whl
或 tar。
看起来它已经记录在这里:
https://github.com/grpc/grpc/tree/master/tools/distrib/python/grpcio_tools#usage
所以你的 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()
我有一些 .proto
gRPC 文件,我想将其编译为 setup.py 脚本的一部分。这需要运行宁from grpc_tools import protoc
并在setup(args)
之前调用protoc
。目标是编译和安装来自 pip install pkgname
的 pb 文件。
例如
# 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', ...],
...
)
这按预期工作,我在我的站点包或 wheel 中获取了 pb 文件,而无需将它们存在于源文件夹中。但是,这种模式意味着我不能天真地 pip install pkgname
从头开始,因为步骤 compile_protobufs
取决于 grpcio-tools
,直到 setup()
才安装。
我可以使用 setup_requires,但是 that is on the chopping block。我可以先安装依赖项(现在我使用 RUN pip install -r build-require.txt && pip install pkgname/
),但似乎仍然应该有一种更简洁的方法。
我是否正确地使用了这个模式,或者我是否遗漏了一些包装惯用语?
我的标准:
- 通常这是 运行 在容器内,因此尽量减少外部依赖
- 我希望每次
pip install
时重新生成 - 这些文件还需要进入任何
.whl
或 tar。
_pb2.py
文件
看起来它已经记录在这里:
https://github.com/grpc/grpc/tree/master/tools/distrib/python/grpcio_tools#usage
所以你的 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()