python - Advice on Structure to solve ImportErrors -
hi i'm new python , think have problem program structure built.
this extract of structure thought of.
. └── asd ├── asd.py ├── __init__.py ├── framework │ ├── importer.py │ └── __init__.py ├── library │ ├── librarymanager.py │ └── __init__.py in reality there many more packages under asd. asd.py meant 'main'. instantiates many objects of different classes contained in other packages, example has several librarymanager objects. importer can import (not only) library files.
i think key flaw following: enable classes within different packages access each others instances of asd reference main asd parameter. files in question:
asd.py:
from asd.framework.importer import importer asd.library.librarymanager import librarymanager class asd(object): def __init__(self): # instantiate several objects self.nodelibmng = librarymanager("dbnodelibrary.txt") self.importer = importer(self) importer.py:
from asd.asd import asd class importer(object): def __init__(self, asdref : asd): self.asdref = asdref def importnode (self,item): # following line shows why want reference construct. self.asdref.nodelibmng.appenditemtolibrarydb(item) now if want run asd.py following error:
importerror: no module named 'asd.framework'; 'asd' not package if run test code within importer.py error:
importerror: cannot import name 'asd' other topics importerror: cannot import name x suggest have circular dependent imports.
so how can solve this, keep behaviour, every class can communicate instances of asd?
edit 0: i'm starting asd.py or importer.py adding
if __name__ == '__main__': asd=asd()
Comments
Post a Comment