Unsolved No module named 'widgets', but it is a directory, not a module
-
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. -
@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
.
. -
@SGaist with the starting
.
the terminal said:from .Model.standard_item import StandardItem ImportError: attempted relative import with no known parent package
-
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 of course! What part of the code would be more useful for you? I could paste it here
-
@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. -
@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. -
@Mindful i have this problem.~
you can explain better how you solve?