2

I am trying to build a PyQt5 GUI Application. I have python3.6 and pyqt5 installed on Centos-8. The directory structure is as follows:

Workspace/
├── app.py
├── Controller
├── Model
└── View
    ├── ContentLayout
    │   ├── UserActions
    │   │   └── ButtonLayout.py
    │   └── UserInput
    │       └── TextInputLayout.py
    ├── FooterLayout
    ├── HeaderLayout
    ├── mainMenu.py

app.py:

# This is main application
from View.mainMenu import MenuDesign

if __name__ == "__main__":
    test = MenuDesign()
    test.genereateLayout()

mainMenu.py:

from ContentLayout.UserInput.TextInputLayout import TextInput
from PyQt5.QtWidgets import QMainWindow

class MenuDesign(QMainWindow):
    def __init__(self):
        super().__init__()
        pass

    def genereateLayout(self):
        self.userinput = TextInput()
        self.userinput.initialize_label()
        self.userinput.initialize_textinput()
        print("Function called from TextInputLayout")

TextInputLayout.py:

# This is class for designing layouts for text input boxes and status bar.
class TextInput:
    """This is main class for user inputs."""
    def __init__():
        pass

    def initialize_label(self):
        print("Function is printing from TextInputLayout")
        pass

    def initialize_textinput(self):
        print("Function is printing textinput from TextInputLayout")
        pass

When I run the application, I am getting following error:

Traceback (most recent call last):
  File "app.py", line 1, in <module>
    from View.mainMenu import MenuDesign
  File "/home/amarjeets/Workspace/View/mainMenu.py", line 2, in <module>
    from ContentLayout.UserInput.TextInputLayout import TextInput
ModuleNotFoundError: No module named 'ContentLayout'

I tried putting __init__.py file in View, ContentLayout and UserInput folders with all permutation/combination still same error.
Is there any rule or general guideline that I need to follow to import a module/class/function in python of a given project folder/file structure like mine?
And
If I also have a file i.e. consolidatedLayout.py in ContentLayout folder with all the other files then how to import modules from ButtonLayout.py or TextInputLayout.py in consolidatedLayout.py

Amarjeet Sharma
  • 168
  • 1
  • 11

1 Answers1

0

I have the following folder-structure:

C:.
│   app.py
│   __init__.py
│
└───View
    │   MainScript.py
    │   __init__.py
    │
    └───Folder1
            sub1.py
            __init__.py

app.py:

from View.MainScript import Test

print(Test.x)
Test.p()

MainScript.py:

from View.Folder1.sub1 import printIt

class Test:
    x = 123
    p = printIt

sub1.py:

def printIt():
    print("printed inside of sub1.py")

When I launch app.pythis is the output:

123
printed inside of sub1.py

Notice that the import inside of MainScript is from View.Folder1.sub1 import printIt where View is the folder "above", that is counted as the module that you're importing from.

Hampus Larsson
  • 3,050
  • 2
  • 14
  • 20
  • Thanks for the reply. So I have to import modules from root path(where main thread is started i.e. app.py) or I need to import from just one folder above? – Amarjeet Sharma Dec 23 '20 at 11:34
  • You have to think of the folder-structure as a module in itself, and you have to specify "the way" to the files you want to import. So yes, you have to look at it as if your files are being imported from the "root" folder. – Hampus Larsson Dec 23 '20 at 12:53