2

I have installed protobuf in Python 3.4 and i pushed the compiled code pb_x_pb2.py into python34 folder. When i enter import pb_x_pb2.py it shows the following error.

>>> import pb_interface_pb2
Traceback (most recent call last):
  File "<pyshell#21>", line 1, in <module>
    import pb_interface_pb2
  File "C:\Python34\pb_interface_pb2.py", line 5, in <module>
    from google.protobuf import reflection
  File "C:\Python34\lib\site-packages\google\protobuf\reflection.py", line 68, in <module>
    from google.protobuf.internal import python_message
  File "C:\Python34\lib\site-packages\google\protobuf\internal\python_message.py", line 848
    except struct.error, e:
                       ^
SyntaxError: invalid syntax
4
  • Did you compile for Python 3? The syntax looks like Python 2. Commented Apr 1, 2015 at 11:40
  • Oh!!! It means I can only use python 2.7?? Commented Apr 1, 2015 at 11:44
  • Protobuffer should be supported under 3.x, too. But if you compiled for Python 2.x, the result will only run under 2.x. How did you compile the Protobuffer file to Python? Commented Apr 1, 2015 at 11:46
  • Thank you.Compilation is done using the binary protoc. Commented Apr 1, 2015 at 11:55

1 Answer 1

2

Protobuf doesn't really support python 3.x. For the imported libraries, try pip install protobuf-py3, a python 3 port of the package. You can also run the 2to3.py script from the Pythonxx\Tools\Scripts folder on the generated file.

Another option would be to download the newest version of the sources, and compile them yourself. I believe that the newest version is mostly compatible with both 2.x and 3.x

Sign up to request clarification or add additional context in comments.

2 Comments

I have tried in python2.7 version itself but still facing same issue.I installed protobuf using 'pip install protobuf'. But when i do 'import protobuf' in idle it shows Import error that no module available. To use that compiled python protobuf code I trust protobuf should be imported. Can you please tell the steps clearly to use the protobuf code, since Iam new here I am unable to follow you.
It sounds like you are not using the idle from the same installation as where you installed it. Do you have multiple versions of python installed?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.