I'm trying to dynamically import a python script foo.py into another executable script, which is in a deeply nested folder. I'm using
import os
sys.path.insert(0, '../../../../.')
from foo import Bar
this works, and I can use Bar happily.
I would like to make the script dynamically determine the folder depth e.g.
import os
root_path = os.path.relpath(os.popen("git rev-parse --show-toplevel").read()).replace("../reponame", ".")
print(root_path) # prints '../../../../.'
sys.path.insert(0, root_path)
from foo import Bar
However this doesn't work, the script complains it can't find Bar when it is run.
Why is this?