I'm having an interesting problem dealing with importing modules in the same folder, which is a little baffling.
Here is a simplified representation of my project directory structure:
├── my_project/
│ ├── __init__.py
│ ├── main.py
│ ├── gui_class.py
│ ├── qt_files/
│ | ├── __init__.py
│ │ ├── ui_file.py
│ │ ├── image_conversion.py
The issue: in my main.py
file, I am instantiating a class from gui_class.py
& calling a method from that class that runs ui_file.py
. This file is converted from a Qt .ui
file using pyuic5
via PyQt5. When I call the method that should create my window, I get an error:
Traceback (most recent call last):
File "main.py", line 12, in <module>
import start_menu
File "~/my_project/gui_class.py", line12, in <module>
from qt_files.ui_file import Ui_File
File "~/my_project/qt_files/ui_file.py", line 166, in <module>
import image_conversion
ModuleNotFoundError: No module named 'image_conversion'
What am I missing here? The image_conversion
module is a dependency of ui_file.py
, and is without a doubt in the same directory as the UI file.
It's worth noting that nothing in ui_file.py
should be changed. It is generated automatically and anything edited will be overwritten upon re-generation.
UPDATE:
The issue may actually be in how I'm importing the UI files in my main.py
file.
from qt_files.ui_file import Ui_File
Is the problem I'm dealing with here how I'm trying to import my UI class? Please excuse my lack of experience with this kind of project management. Google showed me a lot of different answers, but none of them seemed to be straightforward.
** UPDATE 2:**
I tried to use ekhumoro's example by stripping my project into a very minimal representation, and I'm still getting these errors.
├── noulsmatic_test
│ ├── designer
│ ├── icons
│ ├── noulsmatic.py
│ └── package
│ ├── __init__.py
│ ├── start_menu.py
│ └── ui
│ ├── cchmc_logo_rc.py
│ ├── __init__.py
│ ├── ui_calibration.py
│ ├── ui_controlpanel.py
│ ├── ui_dataprocessing.py
│ ├── ui_savedialog.py
│ └── ui_startmenu.py
Simply, noulsmatic.py
calls start_menu.py
in order to set up a very basic, limited-functionality GUI with the 5 UI files in ui
. However, the import lines in start_menu.py
cause Pylint errors (E0401
) and give me the following traceback:
Traceback (most recent call last):
File "noulsmatic.py", line 13, in <module>
from package import start_menu
File "/home/alex/Documents/CCHMC/noulsmatic_test/package/start_menu.py", line 12, in <module>
from ui.ui_startmenu import Ui_StartMenu
ModuleNotFoundError: No module named 'ui'
Why am I still getting this error, even with __init__.py
files and the same import style as in the linked example?
** UPDATE 3: **
I finally solved the import issue with my files in a subdirectory by adding the parent directory to the import line. Maybe I had just missed this in the resources I had read, including ekhumoro's example (linked above). However, I'm still having my original issue, which is importing my resource file.
At the end of each file in the ui
package, there is the line import cchmc_logo_rc
there to make sure the generated resource code is included in the GUI. It seems I can fix this by changing to import package.ui.cchmc_logo_rc.py
. However, because it is code generated by pyuic5
, I'd like to not have to change it. Is there anything I can do with my __init__.py
file or something like that?
Any help is much appreciated.