I am posting this because the answers to this question (Import a module from a relative path) are not usable for those of us likely to be asking the question in the first place.
Suppose I have the following file structure (I have kept the same naming convention from the other question):
C:\dirMain\
dirFoo\
Foo.py
dirBar\
Bar.py
I want to import Bar.py
from within Foo.py
.
Something like this: (My C is showing here, sorry):
# Foo.py
from ../dirBar/Bar import *
Please feel free to mark as a duplicate but do check the other responses first; most of the ones I have seen are overly complicated, don't work, or are incomplete. This is a simple question for which there is hopefully a simple answer.
Things I've tried:
1) The suggestion below from Puffin GDI:
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
result:
NameError: name 'file' is not defined
The solution to this ostensibly lies amidst this answer, where I do not know.
2) This from here: How to import a module given the full path?
import imp
abc = imp.load_source('bar.py', 'C:\dirMain\dirBar.py')
result:
IOError: [Errno 22] Invalid Argument
3) As discussed here: Import a module from a relative path
(first added __init__.py
to /dirBar
)
from ..dirBar import Bar
result:
ValueError: Attempted relative import in non-package