0

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
Darquaise
  • 1
  • 1
  • What version of Python? – 0x5453 Feb 09 '22 at 15:59
  • try adding an empty `__init__.py` file under utils folder – pugi Feb 09 '22 at 16:02
  • I think this is helpful in understanding python's slightly complicated import system. https://stackoverflow.com/questions/14132789/relative-imports-for-the-billionth-time You probably can figure out why you got your error in this link. – user1473249581 Feb 09 '22 at 16:07

2 Answers2

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
alexzander
  • 1,586
  • 1
  • 9
  • 21
  • I just tried to make another folder and there it works, it seems to have a problem with the folder name `utils` for whatever reason :/ Tho I can't tell why. should I update the question to show more code? – Darquaise Feb 09 '22 at 16:04
  • maybe there is a python module somewhere that is called `utils.py` and thats why you get an error – alexzander Feb 09 '22 at 16:05
  • I now changed the name to util and that works, still weird tho – Darquaise Feb 09 '22 at 16:22
1

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.

Elfener
  • 11
  • 2