Importing Python modules from different working directory

Actually your example works because checks.py is in the same directory as agent.py, but say checks.py was in the preceeding directory, eg;

agent/agent.py
checks.py

Then you could do the following:

path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
if not path in sys.path:
    sys.path.insert(1, path)
del path

Note the use of __file__.

Leave a Comment