No module named 'widgets', but it is a directory, not a module
-
Hello everyone! I'm having a problem with PyQt5: I would like to open a .py document, but when I try to open it in my Mac terminal, it doesn't work. The terminal say that there is no module named 'widgets', but 'widgets' is not a module, it's a directory. In the 'widgets' folder there is the 'gui_.py' file that i would like to open. It contain the Ui_MainWindow class that I would like to import.
How can I solve the problem? -
Hi,
Where does that .ui come from ?
How did you generate the Python code from it ?
Which version of PyQt5 are you using ? -
Hi @SGaist, I am using QtDesigner tool from Qt to create the ui files and after the creation of them I am using the command:
pyuic5 -o <the name of the file we wish to generate in .py format> <the name of the ui file generated by QtCreator in ui format>
Ex: pyuic5 -o mygui.py mygui.uipyuic5 comes from python ui compiler (or converter, probably) and 5 which comes from the version of pyqt. For the resources file we are doing the same thing but using the resources compiler from Pyqt5 which is called pyrcc5. For the third question the answer is: Pyqt5 version 5.9.2
-
Can you show the structure of your application ?
It seems there might be some issue with that.If you are using sub folders, did you think about adding the
__init__.py
file in it ? -
Can you show the structure of your application ?
It seems there might be some issue with that.If you are using sub folders, did you think about adding the
__init__.py
file in it ?@SGaist I’m sorry for the latency in answering you but I was trying to solve the problem on my own. Unfortunately I didn’t succeed because even today the terminal said that it can’t find subfolders that are in the main one. In its opinion they don’t exist. “Adding the
__init.py__
file”: what does it mean? -
Literally that: adding a file named
__init__.py
in the folder. However it might not be needed anymore with Python 3. -
Literally that: adding a file named
__init__.py
in the folder. However it might not be needed anymore with Python 3.@SGaist the problem persist, it said that there isn’t any module name “Model”, but Model is a folder. Inside it there is the
Standard_Item
file that i want to import:From Model.standard_item import StandardItem ModuleNotFoundError: No module named ‘Model’
Do you think it could be a venv problem?
-
Shouldn't that be:
from .Model.standard_item import StandardItem
?
Notice the starting
.
. -
Would it be possible for you to provide a minimal project that shows that issue so we can debug that from the same source ?
-
Would it be possible for you to provide a minimal project that shows that issue so we can debug that from the same source ?
-
Would it be possible for you to provide a minimal project that shows that issue so we can debug that from the same source ?
@SGaist here I am. I solved the problem by adding a line of code at the beginning of the main file:
sys.path.append(r’/Users/[user]/Desktop/[folder name]’)
It identifies the path to the folder that contains the other subfolders. Thanks for your time!
-
When using that technique you should rather determine the location of the current script using
__file__
so that you don't hard code the path in it. -
@SGaist here I am. I solved the problem by adding a line of code at the beginning of the main file:
sys.path.append(r’/Users/[user]/Desktop/[folder name]’)
It identifies the path to the folder that contains the other subfolders. Thanks for your time!
@Mindful i have this problem.~
you can explain better how you solve? -
@Mindful i have this problem.~
you can explain better how you solve?@Filipe_Portugal
See https://www.geeksforgeeks.org/file-a-special-variable-in-python/ for__file__
. Use that if you need the path to the Python script file/directory rather than hard-coding it. -
@SGaist of course! What part of the code would be more useful for you? I could paste it here
@Mindful i have this problem.~
you can explain better how you solve?