ModuleNotFoundError: No module named <>

ModuleNotFoundError: No module named <>

我正在尝试 https://developers.google.com/protocol-buffers/docs/pythontutorial,其中我创建了一个这样的目录结构:

└── address_book
    ├── proto
    │   ├── addressbook.proto
    │   └── addressbook_pb2.py
    └── src
        └── write.py

write.py内容:

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

所以现在当我尝试执行 write.py 时,我 运行 出现以下错误:

(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'

试试这个,

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