protocolbuffers / protocolbuffers/protobuf
Python: support for optional python_package (or similar) to make import explicit
@esrauchg is already working on this.
Since Apr 1, 2026.
- Dominant language
- C++
- Stars
- 72k
- Forks
- 16.3k
- Avg merge
- 1d 17h
- Merged PRs (30d)
- 140
Description
What language does this apply to?
Python.
Describe the problem you are trying to solve.
The Python code generator uses the directory structure of the .proto files to generate the generated Python code directory layout and imports (e.g. when Protobuf file A imports Protobuf file B). It doesn't sanitize Python reserved keywords.
This can be problematic in two cases:
-
The directory structure contains a reserved keyword. For example when it contains
as. In pythonimport asfails but alsoimport as.foo.baris invalid. -
When in case of A depending on B, A will contain an absolute import that might not be on the Python path (e.g. when the generated code is packaged so instead of
import bit should doimport mypackage.b). This was mentioned as a fix in the discussions: https://github.com/protocolbuffers/protobuf/issues/1491, but I would consider this a workaround.
Describe the solution you'd like
For several languages, there are ways to make the package / module name explicit by defining an option, e.g. option go_package, option java_package, ... (https://developers.google.com/protocol-buffers/docs/proto3#packages). (I believe there is also a ruby_package https://github.com/protocolbuffers/protobuf/pull/4627).
I believe an optional option python_package could solve the above issues as a as/api.proto file then could contain an option python_package = "as_pb.api" (or "my_package.as_pb.api").
The Protobuf code generator will then take the python_package into account when:
- Generating the directory structure for the generated Python code so that it can be imported as
import as_pb.api(as_pb/api.py). - When defining imports (Protobuf file A depends on B). The generated file A then imports B using the (optional) defined
python_package.
Describe alternatives you've considered
My initial approach was to re-define the structure of the original Protobuf files. However, when the Protobuf files are used to generate other languages bindings, this has side-effects (e.g. the Go code now ends up in as_pb/api, it should be in as/api.
I believe the supported languages should not be leading in how the Protobuf structure looks like.
Additional context
Explicit is better than implicit.
(https://www.python.org/dev/peps/pep-0020/)
😉
See also https://github.com/brocaar/chirpstack-api/issues/5 .
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.