Skip to main content

Plugin for setuptools and grpc / protobuf python module build

Project description

PyPI - Version PyPI - License PyPI - Python Version GitHub Workflow Status (with event)

Plugin for setuptools to compile protobuf and gRPC service files to python modules.

This package provides build_grpc command for setuptools. Its purpose is to build gRPC modules during build step of setuptools packaging.

What this isn’t

This is not a command line script.

Direct invocation of setup.py (such as python setup.py build_grpc) has been deprecated and will be removed in future version of setuptools.

If you’d like to have CLI for building gRPC modules, use grpcio-tools directly (that’s what this package uses under the hood). After installing that package, you can learn about its options by running python -m grpc_tools.protoc --help.

Installation

You probably shouldn’t install this package directly. Instead, you should add it to the build-system.requires in pyproject.toml. See Configuration.

Options

Command build_grpc provides following options:

  • proto_files: Newline separated list of glob patterns matching protobuf files to be compiled. Paths are relative to proto_path. ** can be used to match any files and zero or more directories. Default value is empty list.

  • grpc_files: Newline separated list of glob patterns matching grpc service files to be compiled. Paths are relative to proto_path. ** can be used to match any files and zero or more directories. Default value is empty list.

  • proto_path: Path to root directory with protobuf files. This path is passed through -I option to grpc_tools.protoc. Default is . (current directory).

  • output_path: Path to root directory for generated python modules. This path is passed through --python_out or --grpc_python_out option to grpc_tools.protoc. Default is . (current directory).

Configuration

You have to specify setuptools-grpc as part of build backend requirements. This follows specification introduced in PEP 518. You can read more about it in setuptools docs.

# file: pyproject.toml
[build-system]
requires = ["setuptools", "setuptools-grpc"]
build-backend = "setuptools.build_meta"

Next, you need to configure setuptools_grpc itself. This can be done in setup.py, but we recommend declarative config in setup.cfg. Depending on your project structure, you may not need some of the options below. You’ll always need to specify at least proto_files or grpc_files, otherwise setuptools_grpc won’t do anything.

# file: setup.cfg
[build_grpc]
proto_files = **/*.proto
grpc_files = **/*_grpc.proto
proto_path = ./src
output_path = ./out

It’s also recommended to add generated files to your MANIFEST.in. Otherwise they might be missing from the built wheel. See setuptools docs for more details about how files are included in the distribution.

# file: MANIFEST.in
graft out

Restricting build dependencies

Generated gRPC Python modules have a restriction: You should ideally run the code with the same version of protobuf that was used to build the package. To complicate matters even further, grpcio-tools bundles its own version of protobuf and although it does depend on protobuf package, it doesn’t actually use it to generate the Python modules.

There’s a possible solution: Don’t generate the gRPC Python modules until you know which version of protobuf will be used to run it. If you have a separate package for gRPC API, you can publish the sdist, but not the wheel. Wheel will be built when you install the sdist and you can specify the grpcio-tools version used to build it. To this end, setuptools-grpc offers setuptools_grpc.build_backend. When you use this build backend, env variable SETUPTOOLS_GRPC_BUILD_DEPS will be considered and all of the specified dependencies will be used as additional build dependencies. You can specify multiple dependency restrictions separated by whitespace (don’t use any whitespaces otherwise!).

# file: pyproject.toml
[build-system]
requires = ["setuptools-grpc"]
build-backend = "setuptools_grpc.build_backend"
SETUPTOOLS_GRPC_BUILD_DEPS="grpcio-tools==1.74.0" pip install "my-grpc-api-package" "protobuf==4.31.1"

If you ensure that the specified protobuf version matches the version bundled in grpcio-tools, you’ll run the code with the exaclty same protobuf version that was used to build it.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

setuptools_grpc-1.0.0.tar.gz (20.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

setuptools_grpc-1.0.0-py3-none-any.whl (19.1 kB view details)

Uploaded Python 3

File details

Details for the file setuptools_grpc-1.0.0.tar.gz.

File metadata

  • Download URL: setuptools_grpc-1.0.0.tar.gz
  • Upload date:
  • Size: 20.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.11

File hashes

Hashes for setuptools_grpc-1.0.0.tar.gz
Algorithm Hash digest
SHA256 1fb1ca42be819eb6a72f33d0f75f7d76d8a6af4a89361661a77ea190606fe9d6
MD5 91bff3766bf50c178d2ba90b8f010bff
BLAKE2b-256 6cc63f71ff77dc7934647702a97eb25af8b1db62f2885b0513ffc3e8da5b5d95

See more details on using hashes here.

File details

Details for the file setuptools_grpc-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for setuptools_grpc-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fbd714cfc9d7f0836c8d2f8358d8730ac53473ab444190a6b4d426884bfc9226
MD5 658260da0d8056b507031821c895e895
BLAKE2b-256 41e38075f349233311640887517a0139829539e70cfac76d9d9119c6c1781469

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page