My Directory is structured as follows:
superParent
│ app.py
├───custom_package
file1.py
file2.py
file3.py
file4.py
__init__.py
I am running app.py
I want to import functions from file1.py
and file2.py
into app.py
. I also import functions from file3.py
and file4.py
in file1.py
and file2.py
.
Now when I include
from custom_package.file1 import func_abc
in app.py
it throws an ModuleNotFound
error. However,
from custom_package import file1
func_abc = file1.func_abc
works perfectly.
Similarly, when I am trying to import a function from file3.py
into file1.py
using:
from file3 import func_efg
in file1.py
it throws an ModuleNotFound
error. However,
from custom_package import file3
func_efg = file3.func_efg
works perfectly in file.py
.
I have found a lot of similar questions in StackOverFlow, however, none of them addressed this peculiar (for me - not for everyone) behaviour. A lot of them suggest making changes to the PYTHONPATH
or sys.append('path\to\custom_package')
(while strongly arguing that we should not resort to this). How do we solve these import-related issues to import particular functions from custom-defined local packages from a relative directory (preferably without messing with sys.path
at all)?
Additional Context (not sure if this is necessary): I will be containerizing this and deploying the container to AKS.
Some additional StackOverflow questions which have addressed similar issues: