I am trying to get Protobuf to work with Python but Python throws an error when trying to import a compiled .proto file. The error is ModuleNotFoundError: No module named ‘google’.
This is on a brand new and clean EC2 instance with Ubuntu Linux. Installation of the protocol compiler is by downloading a pre-built binary from the release page and then following the instructions in the readme file, as described here. The full list of commands is shown below. The compilation appears to work but the import fails. What is going wrong? Thanks in advance.
I use scp to copy my_file.proto from the local machine to the EC2 instance. Then SSH to the instance and:
$ wget https://github.com/protocolbuffers/protobuf/releases/download/v21.5/protoc-21.5-linux-x86_64.zip
$ sudo apt install unzip
$ unzip protoc-21.5-linux-x86_64.zip
$ sudo cp bin/protoc /usr/local/bin/.
$ protoc (to test that protoc works)
$ sudo cp -R include/google/ /usr/local/include/.
$ ls /usr/local/include/google/protobuf (to confirm that the copy worked)
$ export SRC_DIR=/home/ubuntu
$ export DST_DIR=/home/ubuntu
$ protoc -I=$SRC_DIR --python_out=$DST_DIR $SRC_DIR/my_file.proto (works)
$ python3
import my_file_pb2 (fails)
The import fails with "ModuleNotFoundError: No module named ‘google’"
$ which python3
returns /usr/bin/python3
2
Answers
Perhaps
pip3 install google-api-python-client
.Or
pip3 install google
, but as I can see it does not seem to work for many people, if it works for you then great.You need to install
protobuf
too.protoc
generates the stubs but Python’s Protobuf implementation has a runtime requirement and that’s provided by theprotobuf
module.It’s a good idea to use e.g.
virtualenv
to compartmentalize Python workspaces.When you’re done
You may also want to keep
protoc
local with: