Allow to use functions in another folder of parent folder
1) By using module structure
# Folder Structure:
root/
serve/
- app.py
src/
- utils.py
# In app.py, use:
from src.utils import function
# RUNNING [from root]:
python -m serve.app
[instead of python serve/app.py]
# Note: -m allows us to run library module as a script.
2) By using system paths
import sys
from os.path import dirname, realpath
parent_dir_name = dirname(dirname(realpath(__file__)))
sys.path.append(parent_dir_name + "/src")
import fileInSrc
3) By using os current working directory and system paths
import os
import sys
sys.path.append(os.getcwd())
3) By only using sys
import sys; sys.path.insert(0, '../src')
# utils folder is in src folder
from utils.helpers import function
4) By using Path Module
import sys
from pathlib import Path
ROOT_DIR = Path(__file__).resolve().parent
sys.path.append(str(ROOT_DIR.parent))
Import a module from sibling directory without Pylance Issues
import sys; sys.path.append('../'); sys.path.append('../src/') # import sibling folder
Reload a spesific function without restarting the kernel
# this way no need to restart the kernel to reload the module
import importlib; my_func = importlib.reload(sys.modules["src.plotter"]).my_func