Sunday, September 17, 2023

Python Import from Another Subfolder

metadict/static/langs.py
metadict/scripts/api.py
Directory metadict/static/ should have __init__.py file

Code in file metadict/scripts/api.py
import sys, os
# current_dir = os.path.dirname(os.path.realpath(__file__))
# parent_dir = os.path.dirname(current_dir)
# sys.path.insert(0, os.path.join(parent_dir, 'static'))
from langs import *

or

import sys
sys.path.insert(0, './static/')
import langs
print(langs.languages)

or

import sys
sys.path.append('./static/')
from langs import *
print(languages)