Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

ModuleNotFoundError: No module named <>

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

(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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading