Giter Club home page Giter Club logo

Comments (8)

echarrod avatar echarrod commented on August 18, 2024

You also have another option other than the one you stated:
Change the subfolder init.py file to include:

import os
import sys
sys.path.insert(0, os.path.abspath(os.path.dirname(__file__)))

from docker-protoc.

ido-namely avatar ido-namely commented on August 18, 2024

Hi @ysyyork sorry for the late reply.
Is this still an issue for you?

from docker-protoc.

maxjakob avatar maxjakob commented on August 18, 2024

I am also observing this issue. A workaround is to call the container with an explicit --grpc-out argument:

docker run --rm \
  -v `pwd`:/project \
  -w /project \
  namely/protoc-all:1.50 \
    -f project.proto \
    -l python \
    -o . \
    --grpc-out ./proto

Now all *.py files are inside proto/ and the imports work.

What I don't like is that __init__.py files are created recursively for each directory starting at ., so this workaround definitely has drawbacks.

@ido-namely Do you think this is something we can fix inside the container?

from docker-protoc.

ido-namely avatar ido-namely commented on August 18, 2024

Hi @maxjakob ,
Thank you for your input. I'll try to take some time to better understand the issue.
I personally haven't used this project to generate protos for python.
if you can clarify in the meantime though - in your example, do project_pb2.py project_pb2_grpc.py still get generated in the same dir?
What is the import command that they are generated with?

from docker-protoc.

maxjakob avatar maxjakob commented on August 18, 2024

@ido-namely After running

$ docker run --rm \
  -v `pwd`:/project \
  -w /project \
  namely/protoc-all:1.50 \
    -f project.proto \
    -l python \
    -o ./ \
    --grpc-out ./proto

I get the following files:

$ ls proto/
project_pb2_grpc.py  project_pb2_grpc.pyi  project_pb2.py  project_pb2.pyi  __init__.py

The import paths in these files are such that the issue reported by @ysyyork goes away, the imports work with this combination of parameters.

However, you now also get __init__.py files in the root and all subdirs recursively:

$ ls .
proto/  otherdir/  __init__.py
$ ls otherdir/
__init__.py

These excess __init__.py files are a problem in our git and linting workflows. Is there some way to turn off the generation of these? Or is there a combination of arguments that both fixes the issue described by @ysyyork and prevents these files from being generated everywhere?

from docker-protoc.

maxjakob avatar maxjakob commented on August 18, 2024

For now, we are going to call protoc directly which also solves this issue:

EDIT: I spoke too soon. This does not solve the import issue.

from docker-protoc.

maxjakob avatar maxjakob commented on August 18, 2024

I think it's actually this issue: grpc/grpc#29459, though I think this is even an issue without proto files importing other proto files. It's already issue with the generated gGRPC file importing the other generated Python file.

See also grpc/grpc#9575 (comment)

from docker-protoc.

maxjakob avatar maxjakob commented on August 18, 2024

I figured out a solution now. If you have the directory structure

project/
  proto/
    project.proto

inside the project/ directory, you can run this command:

$ docker run --rm \
  -v `pwd`:/work \
  -w /work \
  --entrypoint="" \
  namely/protoc-all:1.50_0 \
    bash -c \
      "protoc --python_out=. --mypy_out=. --grpc_out=. --mypy_grpc_out=. --plugin=protoc-gen-grpc=/usr/local/bin/grpc_python_plugin ./proto/project.proto"

and add an __init__.py file yourself:

touch proto/__init__.py

to end up with these files:

project/
  proto/
    __init__.py
    project_pb2_grpc.py
    project_pb2.py
    project_pb2.pyi
    project.proto

The imports in project_pb2_grpc.py will have the form from proto import project_pb2 as ... and will therefore work as expected.

I know that the idea of entrypoint.sh is to abstract away the exact configuration for all the parameters. Maybe this gives somebody enough pointers to fix it there.

from docker-protoc.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.