'ModuleNotFoundError on own folder

I have this folder structure:

main.py
utils
--forms.py

and am now trying in main.py to import something from forms.py with this:

from utils.forms import search_form

but I am getting this Exception

Traceback (most recent call last):
  File "/home/kofi/Desktop/projects/groundtruth_auswertung/main.py", line 4, in <module>
    from utils.forms import search_form
ModuleNotFoundError: No module named 'utils.forms'; 'utils' is not a package


Solution 1:[1]

for me its working and on linux too.


? [~/Alexzander__/programming/python3/learning/modulenotfounderror]
?  tree
? .
??? ? main.py
??? ? utils
   ??? ? forms.py


? [~/Alexzander__/programming/python3/learning/modulenotfounderror]
?  ca main.py

from utils.forms import search_form

print("success")%                                                                      

? [~/Alexzander__/programming/python3/learning/modulenotfounderror]
?  python main.py
success

Solution 2:[2]

Create a file named __init__.py in the utils folder, which allows the folder to be used as a package. (The file doesn't have to have anything inside)

This is only necessary on python 2 though, in python 3 it works automatically without the need for such file.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 alexzander
Solution 2 Elfener