绝对杂质

2024-09-28 18:45:56 发布

您现在位置:Python中文网/ 问答频道 /正文

以下是我的项目结构:

/project_dir
  /src
    __init__.py
    index.py
    /handler
      __init__.py
      base_handler.py

index.py中,我像这样导入base_handler.BaseHandler

from src.handler.base_handler import BaseHandler

出现错误:

ModuleNotFoundError: No module named 'src'

奇怪的是,当我将index.py移出src文件夹时,这个错误就会消失:

/project_dir
  index.py
  /src
    __init__.py
    /handler
      __init__.py
      base_handler.py

现在在index.py中,我可以像这样导入BaseHandler

from src.handler.base_handler import BaseHandler

有人能告诉我为什么会这样吗


Tags: 项目frompyimportsrcprojectbaseindex