Ok, so here is my directory structure
company
└── team
└── user_utils
├── __init__.py
├── test.py
├── utils.py
from the root of my project - which is at /user/user4769/development/python-utils
From the root I run :
python3.8 /user/user4769/development/python-utils/company/team/user_utils/main.py
or the same with -m
flag. As expected, when I print sys.path
the current directory of main.py
is added first - so I do see :
['/user/user4769/development/python-utils/company/team/user_utils', ....]
In my main.py
I do have :
import company.team.user_utils.utils
But I do get ModuleNotFound
If I add this sys.path.append(os.getcwd())
then /user/user4769/development/python-utils/
which is my root folder gets added onto the path, and then import works.
What am I missing? This is Python3.8
The (top) module you are trying to import is company
so for this to work you need to have /user/user4769/development/python-utils/
in your path, as you have also found out. And I believe there also needs to be __init__.py
files in the company
and team
directories. If you want to treat the user_utils
directory as a location to import modules from, as implied by having pointing out it is in your path, a simple import utils
will do.
The dot notation is only for importing submodules and elements from a hierarchical module with submodules. So if company
was the module, with (one) submodule being .team.user_utils.utils
.
Note: given that your import works with .../python-utils
in your path, I'd wager that company
is indeed a hierarchical module. If that is the case, utils.py
may utilise elements from other parts of the company
module, in which case importing it directly and not as a submodule may very well not work.