Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ModuleNotFoundError
#1
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!! :(
Reply
#2
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.
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#3
(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
Reply
#4
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.
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#5
(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.
snippsat likes this post
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#6
(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]
rob101 likes this post
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020