The essence of the problem:

When compiling python code:

#!/usr/bin/env python # -*- coding: utf-8 -*- import socket import message_pb2 import sys sock = socket.socket() sock.connect(('localhost', 8080)) Request = message_pb2.request() Request.a=1 Request.b=2 Request.c=3 for i in [1,2,3]: a = int (input()) sock.send(Request) 

Gives an error message

 Traceback (most recent call last): File "D:\protobuf\client.py", line 5, in <module> import message_pb2 File "D:\protobuf\message_pb2.py", line 6, in <module> from google.protobuf import descriptor as _descriptor ImportError: No module named google.protobuf 

What could be the problem? The protobuff file with the structure was successfully compiled and lies in the folder with the file (where the client.py code client.py )

Thank you in advance

  • Do you have the protobuf package protobuf ? - awesoon
  • The protocol is downloaded from ( github.com/google/protobuf ) completely all and lies in the same folder. - Sergey
  • If you need a different install, please write. - Sergey
  • I actually wrote an hour ago, but I thought I misunderstood your problem and deleted the answer, leaving a clarifying comment. - awesoon

2 answers 2

TL; DR; - use the package manager to install the protobuf package (starting with Python 3.4 and Python 2.7.9, pip is included):

 python -m pip install protobuf 

Why doesn't the method you used work?

Consider the following file structure:

 $ tree . ├── client.py ├── google │  └── __init__.py ├── message_pb2.py └── protobuf.py $ cat client.py import message_pb2 $ cat message_pb2.py from google.protobuf import descriptor as _descriptor $ cat protobuf.py def descriptor(): pass 

When you try to run the client.py file, Python will give the error:

 Traceback (most recent call last): File "client.py", line 1, in <module> import message_pb2 File "/home/soon/Src/Python/test_20412/message_pb2.py", line 1, in <module> from google.protobuf import descriptor as _descriptor ImportError: No module named 'google.protobuf' 

And this is logical, since the google directory contains only the __init__.py file. If we move the protobuf.py file to the google directory:

 $ tree . ├── client.py ├── google │  ├── __init__.py │  └── protobuf.py └── message_pb2.py 

then everything will run without errors.

Why it is worth using a package manager, and not throwing files with your hands?

For the same reason that you install programs through the installation wizard, and do not copy all the files into folders with pens.

  • in cmd I go to the folder where I have client.py, message_pb2.py and where the google folder is, I write in cmd "python -m pip install protobuf" and errors (one of them is an example): Traceback (most recent call last): File "C: \ python27 \ lib \ site-packages \ pip \ basecommand.py", line 223, in main status = self / run (options, args) and the same kind of error (number of 20-30 pieces) - Sergey
  • Update your question by adding a traceback to it. Without it, it is difficult to understand what is happening - awesoon
  • Cannot show directory tree. I think the problem is in the files that you need to add in addition with protobuf. For python - Sergey
  • Traceback (most recent call last): File "D: \ protobuf \ python \ client.py", line 5, in <module> import message_pb2 File "D: \ protobuf \ python \ message_pb2.py", line 10, in < module> from google.protobuf import descriptor_pb2 ImportError: cannot import name descriptor_pb2 here's a new one - Sergey
  • Need Python version and python -m pip freeze exhaust. - awesoon

There was a problem with protobuf versions. They must be identical.