Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
84 views
in Technique[技术] by (71.8m points)

python - Problem when importing modules when files are at different folders

Purpose

I have coded 2 programs that work nicely. Now, I wanted to create a GUI for both programs. My idea was to create a window, where to select the software that you want to run, and then depending which you clicked, a new window will open. This window will be used for asking the user the inputs I was putting manually before to execute both softwares. Then, in this new window the user will click a button and the software will load using the parameters introduced by the user.

Software will run using Qt5 and python 3.

How I proceed

I created 3 files using Qt Designer. One is the main window (main.py, which creates a window called frame_init), which opens a second window when the user clicks one button. This second window is different if it is the first or the second software. For instance, to open software 1 I have:

def open_software_first(self):
    self.software_first = QtWidgets.QMainWindow()
    self.ui = Ui_software_first()
    self.ui.setupUi(self.software_first)
    self.software_first.show()   #the file is called first.py
    frame_init.hide()  

In first.py there is a button that the user can press to start everything:

import A.main as start_functions
def start_first_software(self):
    self.btn_start.setEnabled(False)
    start_functions.start(values) #values are different properties from checkbox, inputlines...

Here, what I did was to put the 3 files in a folder. Create two folders (A for software 1 and B for software 2) and put the files for each software in its own folder. That's why the import has this A. In all folders I placed an empty file called "__ init__.py". Initially, I run the software opening frame_init. For the files commented here, the structure would be:

 application  
  ├── __init__.py
  ├── main.py
  ├── first.py 
  ├── second.py 
  ├── A
      └── __init__.py  
      └── main.py
      └── AAAAAA.py
      └── BBBBBB.py
      └── ...
  ├── B
      └── __init__.py  
      └── ...

Problem

How do imports have to be in main of software 1 in A folder? To import functions in other files in main I was using:

import AAAAA as a
import BBBBB as b

and so on. Now, it doesn't work. I have to put:

import A.AAAAA as a
import A.BBBBB as b

so it's like it is importing from the folder where the GUI is loaded. The problem is that in file AAAAA for example, I have an import to BBBBB:

import BBBBB as b

and in file BBBBB I need AAAAA:

import AAAAA as a

main loads "import A.AAAAA as a". Then it calls a function in AAAAA. This function uses a function in BBBBB. Now when the function in BBBBB is loaded I have an error:

"ModuleNotFoundError: No module named 'AAAAA'". If I put A.AAAAA then it says: "AttributeError: module 'A' has no attribute 'AAAAA'"

How do I have to load this import? I'm sorry not to post here a workable example, but I thing is too messy if I put everything...

question from:https://stackoverflow.com/questions/65865602/problem-when-importing-modules-when-files-are-at-different-folders

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Finally I got working!

Following Sorin's answer, copy that code to each of the files in the subfolder, change the "subfolder" text in his code to the name of your subfolder and then import everything as if it were in the same folder, as always. Do not put in some files "import subfolder.yourpyfile as XXX" and in others "import yourpyfile as XXX". Do always put in all of them "import yourpyfile as XXX". No idea why it was failing, but this works. Do not forget to include those empty init.py files in each folder.

PS. Do not include files inside two folders with same name. The import will load one, but not the other one.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...