Python Example | Import module From Parent Directory

This python example code will introduce how to import python module from its parent directory.

import os, sys
currentdir = os.path.dirname(os.path.realpath(__file__))
parentdir = os.path.dirname(currentdir)
sys.path.append(parentdir)
 
from common import a
 
print("Execution of run starts")
 
a.CommonFunction()
 
print("Execution of run finishes")

In order to import module from parent directory, we should add parent path to sys.path

In this exmaple code, we will use sys.path.append(parentdir) to implement.