I’m trying https://developers.google.com/protocol-buffers/docs/pythontutorial wherein I have created a directory structure like this:
└── address_book
├── proto
│ ├── addressbook.proto
│ └── addressbook_pb2.py
└── src
└── write.py
write.py content:
import os
import sys
PROJECT_ROOT = os.path.abspath(os.path.join(
os.path.dirname(__file__),
os.pardir)
)
sys.path.append(PROJECT_ROOT)
from address_book.proto import addressbook_pb2 as ab
So now when I try to execute write.py, I run into following error:
(venv) 15:53:32:~/Desktop/work/mbip/protobufs % python
address_book/src/write.py Traceback (most recent call last): File
"address_book/src/write.py", line 10, in
from address_book.proto import addressbook_pb2 as ab ModuleNotFoundError: No module named ‘address_book’
>Solution :
try this,
import os
import sys
PROJECT_ROOT = os.path.abspath(os.path.join(
os.path.dirname(__file__),
os.pardir)
)
# print(sys.path)
sys.path.append(PROJECT_ROOT)
# print(sys.path)
#you don't need the parent dir again since you already added it to the path
from proto import addressbook_pb2 as ab