I have an application snork/Hello.py that has a child page snork/pages/1_Info.py and I would like them to import utility functions from a common snork/utils.py module. A relative import from Hello.py like from utils import load_graph works fine, but a similar import from 1_Info.py like from ..utils import load_graph fails with ImportError: attempted relative import with no known parent package.
I can update the path with sys.path.insert(1, os.path.realpath(os.path.pardir)) before the import, but that gets inserted every time the page runs. I can test to see if itās there first, but that seems awkward, and maybe thereās a better way.
The load_graph() function is cached and pretty simple, there will be lots of other methods used in multiple pages:
Can you share the directory tree structure you have?
I have recreated to my best knowledge from your description below. Ensure that every directory has an __init__.py file to mark them as modules and allow relative imports.
Iām tried moving utils.py into the pages directory and replacing the import in Hello.py to from pages.utils import load load_graph which works, but then utils shows up as a page. I tried renaming it to _utils.py thinking itāll skip the page, but it still shows it (blank, of course). I tried replacing pages/__init__.py with utils.py and in Hello.py from pages import load_graph works, but in 1_Info.py from __init__ import load_graph doesnāt work.
Iāll stick to the āhumphā and update sys.path until thereās a canonical solution.