How to fix ModuleNotFoundError: No module named 'parts'

How to fix ModuleNotFoundError: No module named 'parts'

我有以下文件结构:

mytests
   models
      parts
        __init__.py
        ...
      __init__.py
      model.py
   test.py

这是__init__.py的内容:

from parts import *
from models import My_transformer

if __name__=='__main__':
    # ...

这是model.py的内容:

from parts.attention import Pre_Net

class My_transformer(tf.keras.Model):
    def __init__(self,config,logger=None):
        super(My_transformer, self).__init__()
        ...

当我 运行 test.py 时,出现以下错误:

/mytests/models/__init__.py in <module>()
----> 1 from parts import *
ModuleNotFoundError: No module named 'parts'

在这一行:

from models import My_transformer

我该如何解决这个错误?我 运行 来自 Jupyter Notebook 的 test.py。

更新

我在 parts 里面有 __init__.py,它看起来如下:

from attention import *

if __name__=='__main__':
    print('ok')

你应该有这样的结构和导入:

mytests
   models
      parts
        __init__.py
        attention.py
      __init__.py
      model.py
   test.py

models/parts/__init__.py内容:

from .attention import Pre_Net
# or from .attention import *

models/parts/attention.py内容:

class Pre_Net:
    pass

models/__init__.py内容:

from .model import *

models/model.py内容:

from models.parts import Pre_Net

class My_transformer(tf.keras.Model):
    def __init__(self,config,logger=None):
        super(My_transformer, self).__init__()
        ...

test.py内容:

from models import My_transformer