Python Forum
ModuleNotFoundError - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: ModuleNotFoundError (/thread-38022.html)



ModuleNotFoundError - Kjaglewicz - Aug-22-2022

So the path is:

Output:
secure-erp\ controller\ hr_controller.py main_controller.py model\ hr\ hr.py util.py erp.py
In file hr.py i need to import a function from util.py. So the code is
from model import util
but it gives me an error
Error:
"ModuleNotFoundError: No module named 'model'"
I've tried to add __init__.py file, but it doesn't work.

What's the funniest? When in file erp.py i wrote a line:
from controller import main_controller
everything works perfect.

Please HELP!! :(


RE: ModuleNotFoundError - rob101 - Aug-23-2022

Try from model.hr import util

I think that will work for you, but I'm a little confused by your directory structure, as you seem to have directories and files of the same name.


RE: ModuleNotFoundError - Kjaglewicz - Aug-23-2022

(Aug-23-2022, 01:20 AM)rob101 Wrote: Try from model.hr import util

I think that will work for you.

unfortunately not :(

the directories are:
Output:
\model\util.py and model\hr\hr.py



RE: ModuleNotFoundError - rob101 - Aug-23-2022

Well, the . should traverse the directories (maybe you can use more than one?), and the final 'name', should be the Python file, but I've never gone more than one deep, with directory names.



to add...

I've just done from rob.rob1.rob2 import hello which works on my system, each name.name.name are the directory names.


RE: ModuleNotFoundError - rob101 - Aug-23-2022

(Aug-23-2022, 01:23 AM)Kjaglewicz Wrote: unfortunately not :(

the directories are:
Output:\model\util.pyand model\hr\hr.py

Well, that should be from model import util and from model.hr import hr I think.


RE: ModuleNotFoundError - snippsat - Aug-23-2022

(Aug-23-2022, 01:47 AM)rob101 Wrote: Well, that should be from model import util and from model.hr import hr I think.
Yes that should be right.

Kjaglewicz is do it like this easier to see the structure.
secure-erp\
  controller\
    |-- hr_controller.py
    |-- main_controller.py
  model\
    |-- util.py
    hr\
      |-- hr.py

A quick test.
util.py
def util_func():
    print('util func')
hr.py
def hr_func():
    print('hr stuff')
(secure-erp) G:\div_code\secure-erp
λ ptpython
>>> from model import util
>>>
>>> util.util_func()
util func
>>>
>>> from model.hr import hr
>>>
>>> hr.hr_func()
hr stuff
>>>
I usually make virtual envirmont then there is no OS Path problems.
Also install ptpython then get autocomplete when try out imports,then can see earlier what it find or not.
[Image: IfMDK4.png]