0

Hi I'm new to python and think I have a problem with the program structure I built.

This is an extract of the structure I thought of.

.
└── asd
    ├── asd.py
    ├── __init__.py
    ├── Framework
    │   ├── importer.py
    │   └── __init__.py
    ├── Library
    │   ├── libraryManager.py
    │   └── __init__.py

in reality there are many more packages under asd. asd.py is meant to be the 'main'. It instantiates many objects of the different classes contained in the other packages, for example it has several libraryManager objects. The importer can import (not only) library files.

I think my key flaw is the following: To enable the classes within the different packages to access each others instances of asd they get a reference to the main asd as a parameter. Those are the files in question:

asd.py:

from asd.Framework.importer import importer
from 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):
        # The following line shows why I want this reference construct.
        self.asdRef.nodeLibMng.appendItemToLibraryDB(item)

Now if I want to run the asd.py I get the following Error:

ImportError: No module named 'asd.Framework'; 'asd' is not a package

If I run a test code within the importer.py I get this error:

ImportError: cannot import name 'asd'

other topics like ImportError: Cannot import name X suggest that I have circular dependent imports.

So how can I solve this, but keep the behaviour, that every class can communicate with the instances of asd?

EDIT 0: I'm starting the asd.py or importer.py by adding

if __name__ == '__main__': 
    asd=asd()
AnyGuy
  • 71
  • 1
  • 5
  • 1
    You have the `asd` directory on your path, not the *parent directory* of `asd`. So `asd.py` is found as a top-level module, rather than the directory. – Martijn Pieters Sep 12 '17 at 14:50
  • Are you using `asd.py` as a script? If so, don't. You can't run a single file inside a package as a script, because that then immediately becomes a top-level directory on the path. Put scripts *outside* packages, or use the `python -m` switch to run the module as a script instead (packages that have a `__main__.py` module can be executed using that method). – Martijn Pieters Sep 12 '17 at 14:51
  • You have far too many things called "asd" here. – Daniel Roseman Sep 12 '17 at 14:52
  • I'm starting the asd or importer by adding`if __name__ == '__main__':` and simply calling `asd=asd()` for testing – AnyGuy Sep 12 '17 at 15:03

1 Answers1

0

For anybody who has the same issue: I simply abandoned the specification of the input type of the reference and trust myself and others to give the correct reference. Additionally I wrapped all accesses to the reference in try: except: blocks.

So in the example above:

class importer(object):
    # compare to the previous def __init__(self, asdRef : asd):
    def __init__(self, asdRef):
        self.asdRef = asdRef

    def importNode (self,item):
        # The following line shows why I want this reference construct.
        try:
            self.asdRef.nodeLibMng.appendItemToLibraryDB(item)
        except:
            raise
AnyGuy
  • 71
  • 1
  • 5