gRPC Python Tools provides the protocol buffer compiler `protoc` and a plugin for generating server and client code from `.proto` service definitions. Current version: 1.78.0. Release cadence: Regular updates aligned with gRPC releases.
pip install grpcio-toolsVerified import paths — ran on the pinned version, not inferred.
Generate Python code from .proto files and implement server and client using the generated code. Detailed steps are available in the gRPC Python Quick Start guide.
Use matching versions of grpcio and grpcio-tools.
Always specify the .proto file when running protoc.
Avoid running pip as the 'root' user. It is recommended to use a virtual environment or explicitly acknowledge the action with `--root-user-action=ignore` if the implications are understood.
Ensure `grpcio-tools` is installed in your active Python environment: `pip install grpcio-tools`
Instead of calling `protoc` directly, use the Python module runner: `python -m grpc_tools.protoc [OPTIONS] [PROTO_FILES]`
Install the required build tools and Python development headers for your operating system. For Debian/Ubuntu: `sudo apt-get install build-essential python3-dev`. For Fedora: `sudo dnf install @development-tools python3-devel`. For macOS: Install Xcode Command Line Tools: `xcode-select --install`.
Ensure the directory containing your generated `.py` files is treated as a Python package (e.g., by adding an `__init__.py` file). When invoking `protoc`, use relative paths for imports and output, and consider structuring your project such that generated files reside in an importable package. A common manual fix is to change `import my_service_pb2 as my_service__pb2` to `from . import my_service_pb2 as my_service__pb2` in the `_grpc.py` file. Alternatively, adjust the `protoc` command with appropriate `-I` and output directories.
Add the include path to Google's well-known types when running `protoc`. This path is usually found within your `grpcio-tools` installation. You can typically find it by appending `-I$(python -c 'import grpc_tools; print(grpc_tools.__path__[0])')/_proto` to your `protoc` command, or explicitly specify the path like `-I/path/to/venv/lib/pythonX.Y/site-packages/grpc_tools/_proto`.