Add relative path to system path in Python

When runing pyton scripts, in order to import function from some file, we need to make sure that file is in the system path.
So we can add a path in the code. While adding absolute path is easy, but it may not importable to other people.
So here we show an example, where I want to add a relative path of 3 levels up than the file that’s being current running:

import sys
import os

# Get the directory of the current script
current_dir = os.path.dirname(__file__)

# Get the path to the root directory by navigating 3 levels up
root_path = os.path.abspath(os.path.join(current_dir, '..', '..','..'))

# Add the src directory to sys.path

Author: robot learner
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint policy. If reproduced, please indicate source robot learner !