If all occurring directories are Python packages, i.e. they all contain __init__.py, then you can use
from ..bar_dir import bar
If the directories aren't Python packages, you can do this by messing around with sys.path, but you shouldn't.
36
votes
You can use the sys and os modules for generalized imports. In foo.py start with the lines
import sys
import os
sys.path.append(os.path.abspath('../bar_dir'))
import bar
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.OkRead more