https://github.com/c-bata/grpc-issue-proto-finder
A repository for an issue report of gRPC Python library.
https://github.com/c-bata/grpc-issue-proto-finder
Last synced: about 1 year ago
JSON representation
A repository for an issue report of gRPC Python library.
- Host: GitHub
- URL: https://github.com/c-bata/grpc-issue-proto-finder
- Owner: c-bata
- Created: 2021-07-07T09:03:18.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2021-07-08T11:40:55.000Z (almost 5 years ago)
- Last Synced: 2025-02-10T01:46:08.391Z (over 1 year ago)
- Language: Python
- Homepage: https://github.com/grpc/grpc/issues/26631
- Size: 11.7 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# for https://github.com/grpc/grpc/issues/26631
## Problem Details
I found a bug that it raises ModuleNotFoundError by importing a protoc-generated module after upgraded `grpcio` to `1.38.1` from `1.33.2`.
An error trace is like this:
```python
# Although this program won't raise an ModuleNotFoundError on grpcio==1.33.2,
# this program will raise an ModuleNotFoundError on grpcio==1.38.1.
from foo import a_pb2_grpc
print("success")
```
```
$ pip freeze | grep grpcio
grpcio==1.33.2
grpcio-tools==1.33.2
$ python example1.py
success
$ pip install -U grpcio grpcio-tools
$ pip freeze | grep grpcio
grpcio==1.38.1
grpcio-tools==1.38.1
$ python example1.py
Traceback (most recent call last):
File "/Users/a14737/sandbox/protoc-bug-report-proto-finder/example1.py", line 5, in
from foo import a_pb2_grpc
File "/Users/a14737/sandbox/protoc-bug-report-proto-finder/foo/a_pb2_grpc.py", line 5, in
import a_pb2 as a__pb2
ModuleNotFoundError: No module named 'a_pb2'
```
This error is raised by an import lines in `a_pb2_grpc.py`, protoc-generated Python module:
```python
# Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT!
"""Client and server classes corresponding to protobuf-defined services."""
import grpc
import a_pb2 as a__pb2 # ModuleNotFoundError is raised here!!!
```
In `grpcio==1.33.2`, it won't raise an exception because `grpc_tools.protoc.ProtoFinder` is set on `sys.meta_path` at `import grpc`.
But ProtoFinder is not at `grpcio==1.38.1` due to the change of https://github.com/grpc/grpc/pull/24993. If I explicitly imports `grpc_tools.protoc` before importing `a_pb2_grpc`, ModuleNotFoundError won't be raised.
```python
# This works fine with grpcio==1.38.1 because this program explicitly imports
# "grpc_tools.protoc" so that "grpc_tools.protoc.ProtoFinder" is set in sys.meta_path.
import grpc_tools.protoc
from foo import a_pb2_grpc
print("success")
```
```
(venv) $ pip freeze | grep grpcio
grpcio==1.38.1
grpcio-tools==1.38.1
(venv) $ python example2.py
success
```
I think this is a regression bug which introduced by https://github.com/grpc/grpc/pull/24993,
and we need to import `grpc_tools.protoc` from the `grpc` package as in the previous version.
## Solution
This problem will be resolved by the following patch:
https://github.com/grpc/grpc/compare/master...c-bata:python-proto-finder?expand=1
But I'm not sure if it is the correct solution.
## Related issues and pull requests.
* https://github.com/protocolbuffers/protobuf/issues/1491#issuecomment-772720912
* https://github.com/protocolbuffers/protobuf/pull/7470