简体   繁体   中英

How to fix ModuleNotFoundError: No module named 'parts'

I have the following files structure:

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

This is the content of __init__.py :

from parts import *
from models import My_transformer

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

This is the content of 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__()
        ...

When I run test.py , I get the following error:

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

at this line:

from models import My_transformer

How can I fix this error? I run the test.py from Jupyter Notebook.

Update

I have __init__.py inside parts and it looks as follows:

from attention import *

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

You should have structure and imports like these:

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

models/parts/__init__.py content:

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

models/parts/attention.py content:

class Pre_Net:
    pass

models/__init__.py content:

from .model import *

models/model.py content:

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 content:

from models import My_transformer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM