Import a module from a relative path. Learn Python at Python ...
python.engineering › 279237-import-a-module-from-aAug 22, 2021 · import os, sys, inspect # realpath() will make your script run, even if you symlink it :) cmd_folder = os.path.realpath(os.path.abspath(os.path.split(inspect.getfile( inspect.currentframe() ))[0])) if cmd_folder not in sys.path: sys.path.insert(0, cmd_folder) # Use this if you want to include modules from a subfolder cmd_subfolder = os.path.realpath(os.path.abspath(os.path.join(os.path.split(inspect.getfile( inspect.currentframe() ))[0],"subfolder"))) if cmd_subfolder not in sys.path: sys ...
How to import Python from relative path | ProgrammerAH
programmerah.com › how-to-import-python-fromconsidering that compontent and libs are folders at the same level, we can add the following code directly in code.py to add the parent folder of the current folder to the system path. import sys from os import path sys.path.append( path.dirname( path.dirname( path.abspath(__file__)))) or the following (this is true for any relational folder, as long as we give the absolute path to the folder in lib_path) :