0

Assuming I have this 3 directory structure.

  • Dir_A
    • Dir_B
      • b.py
    • Dir_C
      • c.py

So Dir_B and Dir_C are subdirectories of Dir_A and they each contain 1 .py file. b.py needs to import c.py and I want to be able to run b.py while I"m in either the Dir_B directory or the Dir_A directory. What is the cleanest/simplest way to do the import of b.py? I know I can modify the sys.path but not sure how to do it and the import cleanly and portably so it works in both of these cases.

martineau
  • 119,623
  • 25
  • 170
  • 301
wdog
  • 5
  • 4
  • If I use the accepted answer in the above link then I get this error: "ValueError: Attempted relative import in non-package". By the way, I do have empty __init__.py files in all 3 directories. – wdog Feb 06 '20 at 00:04
  • and if i use this answer: import sys import os sys.path.append(os.path.abspath('../Dir_C')) import c then it works when I execute "python b.py" from Dir_B but the import does not work when I execute "python Dir_B/b.py" from Dir_A. The import fails in that case because it is relative to Dir B not Dir_A. So what I"m asking is how to make it work for both of these cases not one or the other. – wdog Feb 06 '20 at 00:23
  • the answers I'm referring to are the ones at this link:https://stackoverflow.com/questions/10272879/how-do-i-import-a-python-script-from-a-sibling-directory – wdog Feb 06 '20 at 00:24
  • Please reopen this. I have explained why neither of the answers in the link referenced in the closure do not solve this. I don't know what else to do to explain. – wdog Feb 06 '20 at 00:35

0 Answers0