| Crates.io | python-proto-importer |
| lib.rs | python-proto-importer |
| version | 0.1.4 |
| created_at | 2025-08-11 11:13:08.383427+00 |
| updated_at | 2025-08-11 11:13:08.383427+00 |
| description | Rust-based CLI to streamline Python gRPC/Protobuf workflows: generate code, stabilize imports, and run type checks. |
| homepage | https://github.com/K-dash/python-proto-importer |
| repository | https://github.com/K-dash/python-proto-importer |
| max_upload_size | |
| id | 1789923 |
| size | 219,188 |
A Rust-powered CLI that brings production-grade reliability to Python gRPC/Protobuf code generation. Generate, validate, and maintain protobuf-based Python code with confidence.
ζ₯ζ¬θͺη: ζ₯ζ¬θͺγγγ₯γ‘γ³γ
The Core Problem: Standard protoc generates Python files with absolute imports (e.g., import foo.bar_pb2), which breaks when you move generated code to different locations in your project. This forces you to place generated files in specific locations, limiting your project structure flexibility.
Our Solution: Automatically converts all imports to relative imports based on your pyproject.toml configuration. This means you can place generated code anywhere in your project structure and it will work correctly. While other tools exist to address this problem, our integrated approach ensures it works seamlessly with the rest of the validation pipeline.
Unlike standard protoc workflows, this tool validates generated code before it reaches your project. When mypy-protobuf or complex proto definitions produce incorrect stubs, you'll know immediatelyβnot when your CI fails.
The Problem: Standard protoc/mypy-protobuf can generate broken .pyi files that crash your type checker with cryptic errors.
Our Solution: Every generation run includes automatic type validation, ensuring the generated code is correct before you use it.
Generation-time errors vs. Usage-time errorsβknow the difference instantly.
Your project's mypy/pyright settings might not be ideal for validating generated protobuf code. This tool maintains its own optimized type-checking configuration specifically tuned for protobuf validation, ensuring consistent quality regardless of your project settings.
Built with Rust for maximum performance. File operations, import rewriting, and validation run at native speed, making it ideal for large codebases and CI/CD pipelines where every second counts.
# Via pip (recommended)
pip install python-proto-importer
# Via cargo
cargo install python-proto-importer
pyproject.toml configuration:[tool.python_proto_importer]
inputs = ["proto/**/*.proto"] # Your proto files
out = "generated" # Output directory
proto-importer build
That's it! Your generated code is now validated and ready to use.
google.protobuf__init__.py generation: No more missing init files breaking importsproto-importer doctor diagnoses your environmentpyproject.tomlproto-importer buildGenerate Python code from proto files with full validation pipeline.
proto-importer build # Standard build
proto-importer build --no-verify # Skip verification
proto-importer build --pyproject custom.toml # Custom config
proto-importer doctorDiagnose your environment and check dependencies.
proto-importer doctor
Output shows:
proto-importer checkRun verification only (no generation).
proto-importer check
proto-importer cleanRemove generated output directory.
proto-importer clean --yes
All configuration lives in pyproject.toml under [tool.python_proto_importer].
| Option | Type | Default | Description |
|---|---|---|---|
inputs |
array | [] |
Glob patterns for proto files to compile |
out |
string | "generated/python" |
Output directory for generated files |
include |
array | ["."] |
Proto import paths (protoc's --proto_path) |
python_exe |
string | "python3" |
Python executable ("python3", "python", "uv") |
| Option | Type | Default | Description |
|---|---|---|---|
mypy |
boolean | false |
Generate .pyi stubs via mypy-protobuf |
mypy_grpc |
boolean | false |
Generate gRPC stubs (_grpc.pyi) |
Configure under [tool.python_proto_importer.postprocess]:
| Option | Type | Default | Description |
|---|---|---|---|
relative_imports |
boolean | true |
Convert to relative imports |
create_package |
boolean | true |
Create __init__.py files |
exclude_google |
boolean | true |
Don't rewrite google.protobuf imports |
pyright_header |
boolean | false |
Add Pyright suppression headers |
Configure under [tool.python_proto_importer.verify]:
[tool.python_proto_importer.verify]
mypy_cmd = ["mypy", "--strict", "generated"]
pyright_cmd = ["pyright", "generated/**/*.pyi"]
[tool.python_proto_importer]
inputs = ["proto/**/*.proto"]
out = "generated"
[tool.python_proto_importer]
backend = "protoc"
python_exe = "uv" # or ".venv/bin/python"
include = ["proto"]
inputs = ["proto/**/*.proto"]
out = "src/generated"
mypy = true
mypy_grpc = true
[tool.python_proto_importer.postprocess]
relative_imports = true
create_package = true
exclude_google = true
[tool.python_proto_importer.verify]
# Focus on .pyi files to avoid noise from generated .py files
pyright_cmd = ["uv", "run", "pyright", "src/generated/**/*.pyi"]
mypy_cmd = ["uv", "run", "mypy", "--strict", "src/generated"]
[tool.python_proto_importer]
inputs = ["proto/**/*.proto"]
out = "generated"
[tool.python_proto_importer.postprocess]
create_package = false # No __init__.py files
include vs inputsThis is crucial for correct configuration:
include - Search PathsWhere protoc looks for .proto files and their dependencies.
inputs - Files to CompileWhich .proto files to actually compile (glob patterns).
project/
βββ pyproject.toml # Configuration file (the reference point)
βββ api/
β βββ service.proto # Your service (compile this)
βββ third_party/
β βββ google/
β βββ api/
β βββ annotations.proto # Dependency (don't compile)
βββ generated/ # Output here
[tool.python_proto_importer]
include = [".", "third_party"] # Can see all protos
inputs = ["api/**/*.proto"] # Only compile your protos
out = "generated"
Key Point: Dependencies need to be in include (so protoc can find them) but NOT in inputs (you don't want to regenerate them).
uv is a fast Python package manager:
[tool.python_proto_importer]
python_exe = "uv"
[tool.python_proto_importer.verify]
mypy_cmd = ["uv", "run", "mypy", "--strict", "generated"]
pyright_cmd = ["uv", "run", "pyright", "generated"]
- name: Setup
run: |
pip install python-proto-importer grpcio-tools mypy-protobuf
- name: Generate Proto
run: proto-importer build
- name: Run Tests
run: pytest tests/
repos:
- repo: local
hooks:
- id: proto-build
name: Build Proto Files
entry: proto-importer build
language: system
files: \.proto$
Check package structure:
proto-importer check
Verify include paths cover all dependencies
Ensure PYTHONPATH includes the parent of your output directory
For generated .py files with dynamic attributes, focus type checking on .pyi files:
[tool.python_proto_importer.verify]
pyright_cmd = ["pyright", "generated/**/*.pyi"] # Only check stubs
When using multiple include paths with same-named files:
inputs patternsThe tool follows a comprehensive pipeline to ensure reliable code generation:
graph TD
A["proto-importer build"] --> B["π Load pyproject.toml"]
B --> C["π Discover .proto files"]
C --> D["βοΈ Execute protoc"]
D --> E["π¦ Generate Python files"]
E --> F{{"Post-processing"}}
F --> G["π Convert absolute β relative imports"]
F --> H["π Create __init__.py files"]
F --> I["π§ Add type suppressions"]
G --> J{{"β
Verification"}}
H --> J
I --> J
J --> K["π§ͺ Import dry-run test"]
J --> L["π Type checker validation"]
K --> M["Success!"]
L --> M
style A fill:#e1f5fe
style M fill:#e8f5e8
style F fill:#fff3e0
style J fill:#fce4ec
graph LR
A["Generated Code<br/>absolute imports"] --> B["Transformation"] --> C["Final Code<br/>relative imports"]
A1["import api.service_pb2<br/>from api import user_pb2"] --> B1["Import Rewriter"] --> C1["from . import service_pb2<br/>from . import user_pb2"]
style A fill:#ffebee
style B fill:#e3f2fd
style C fill:#e8f5e8
protoc with your configuration__init__.py filesprotoc backend (buf support planned for v0.2)_pb2.py, _pb2_grpc.py, .pyi files).py files (.pyi validated via type checkers)We welcome contributions! See CONTRIBUTING.md for development setup.
# Install development dependencies
cargo install cargo-make
# Run full validation
makers all # Runs format, lint, build, test
# Or individually
makers format
makers lint
makers test
Apache-2.0. See LICENSE for details.