Error with pb2 file (with python 3)

3,618 views
Skip to first unread message

ericYoon

unread,
Feb 3, 2017, 2:12:27 AM2/3/17
to grpc.io
Hello,

I am new to python and also grpc. So this might be my fault.

My grpc tool generates pb2 and pb2_grpc file into "someDir/src/adminServer/" where my working directory is "src".

In this case, my IDE and python 3.5 command line display error like the following:

Error with pb2 file:

File "/home/eric/dev/myproject/src/adminServer/adminServer_pb2_grpc.py", line 6, in <module>
   
import adminServer_pb2 as adminServer__pb2
ImportError: No module named 'adminServer_pb2'

--------------

If I change the line 6 into the following, error disappears: 

from adminServer import adminServer_pb2 as adminServer__pb2

---

I guess these errors were raised due to  python 3 import rule (these worked with python 2).

If there is a way to make grpc tool generate code compatible to python 3, it will be wonderful...

Could anyone answer?

Nathaniel Manista

unread,
Feb 7, 2017, 12:38:13 PM2/7/17
to ericYoon, grpc.io
On Thu, Feb 2, 2017 at 11:12 PM, ericYoon <hyunsi...@samsung.com> wrote:
I am new to python and also grpc. So this might be my fault.

My grpc tool generates pb2 and pb2_grpc file into "someDir/src/adminServer/" where my working directory is "src".

Are you able to share with us the command you're running for this code generation?

In this case, my IDE

What IDE?

and python 3.5 command line display error like the following:

Error with pb2 file:

File "/home/eric/dev/myproject/src/adminServer/adminServer_pb2_grpc.py", line 6, in <module>
   
import adminServer_pb2 as adminServer__pb2
ImportError: No module named 'adminServer_pb2'

--------------

If I change the line 6 into the following, error disappears: 

from adminServer import adminServer_pb2 as adminServer__pb2

---

I guess these errors were raised due to python 3 import rule (these worked with python 2).

If there is a way to make grpc tool generate code compatible to python 3, it will be wonderful...

Could anyone answer?

We're not yet aware of a circumstance in which generated code is valid in Python 2 and invalid in Python 3.

Are you able to share with us your .proto file(s)? Are you able to share with us the smallest example code you can create that demonstrates the problem?

Our apologies for the trouble,
-Nathaniel

Leonardo Romor

unread,
Mar 1, 2017, 11:41:04 AM3/1/17
to grpc.io, hyunsi...@samsung.com
Same problem here, the _pb2_grpc.py is generated in a folder of my project toghether with the _pb2.py.
The _pb2_grpc.py tries to import the _pb2 from the python path (ie: import generated.interface_pb2 as generated_dot_interface__pb2)

Nathaniel Manista

unread,
Mar 1, 2017, 12:55:21 PM3/1/17
to Leonardo Romor, grpc.io, ericYoon
On Wed, Mar 1, 2017 at 8:41 AM, Leonardo Romor <leonard...@gmail.com> wrote:
Same problem here, the _pb2_grpc.py is generated in a folder of my project toghether with the _pb2.py.
The _pb2_grpc.py tries to import the _pb2 from the python path (ie: import generated.interface_pb2 as generated_dot_interface__pb2)

You may wish to subscribe to issues 9450 and 9575 which I suspect are related to this.

Let me ask of you the same question I ask in 9450: given that we have test coverage of our code generation that runs and passes with Python 3, what's wrong with our test? What's the smallest code-and-instructions corpus you can share with us that demonstrates the problem you're having?

If you are finding that generated code works with Python 2 and fails with Python 3, are you able to recommend a fix that would preserve the "same code is generated for both Python 2 and Python 3" property in place today, or would you recommend that different code be generated for different Python versions?
-N

ericYoon

unread,
Mar 1, 2017, 10:51:17 PM3/1/17
to grpc.io, hyunsi...@samsung.com
Hi, sorry for late reply.

The IDE i am using is Eclipse 4.6.2 with pydev (PyDev for Eclipse 5.4.0.201611281236)

When I run python 3.5 and try to import the same error occurs.

eric@diot:~/src/SPT_DIOT/snooopy/src$ PYTHONPATH=. python3
Python 3.5.2 (default, Nov 17 2016, 17:05:23)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from ccodeServer.grpcComm import ccodeMan_pb2
>>> from ccodeServer.grpcComm import ccodeMan_pb2_grpc
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 File "/home/eric/src/SPT_DIOT/snooopy/src/ccodeServer/grpcComm/ccodeMan_pb2_grpc.py", line 6, in <module>
   import ccodeMan_pb2 as ccodeMan__pb2
ImportError: No module named 'ccodeMan_pb2'

After fixing import manually 
Auto generated: import ccodeMan_pb2 as ccodeMan__pb2
Manually fixed: from ccodeServer.grpcComm import ccodeMan_pb2 as ccodeMan__pb2

>>> from ccodeServer.grpcComm import ccodeMan_pb2_grpc
>>>

No more error occured.

This might be my path issue but IMHO, import path does not accept relative path from Pyhon 3 (in Pyhon 3, relative import path was accepted so there was no such issue)

Regards,

Nathaniel Manista

unread,
Mar 21, 2017, 2:53:56 PM3/21/17
to ericYoon, grpc.io
On Wed, Mar 1, 2017 at 7:51 PM, ericYoon <hyunsi...@samsung.com> wrote:
Hi, sorry for late reply.

Right back at you. :-)
You may be using Eclipse with PyDev, but you're executing your _pb2.py and _pb2_grpc.py code generation at the command line outside of your IDE, right? What is the exact command that you're running to generate your code?
-Nathaniel
Reply all
Reply to author
Forward
0 new messages