Looking for a way to import common.py into read.py and write.py without pasting boiler-plate code in each script, and without any clunky sys.path foolery at the top of each script. I have already tried littering __init__.py everywhere.
Dir structure:
parent/
common.py
script1/
config.json
read.py
write.py
...
script2/
config.json
read.py
write.py
...
script3/
config.json
read.py
write.py
...
...
They should all import code like this from common.py:
import sys
import logging
from pathlib import Path
import uvloop
file_path = Path(__file__).resolve() # remove /../ in path
project_dir = file_path.parent.parent.parent
data_dir = project_dir.parent / 'data'
# Allow imports from project dir
sys.path.insert(1, str(project_dir))
def init_stuff(_file_, logger_name=None):
uvloop.install()
lg = logging.getLogger(logger_name)
...
return stuff
Again, to re-iterate once more, I'm looking for a better solution than having to use sys.path.