Understand Python Path — Import Variable From __init__.py (update)


This is an update for How to (Python) get value from __init__ in same dir | by 洪健翔 Hung, Chien-hsiang | Aug, 2022 | Medium. Make sure you have read the previous one. Let’s get started!

Photo by AltumCode on Unsplash

Dir Structure:


| — __init__.py

| — bar.py


colour_map_earth_tone = ['#E8D0A9', '#B7AFA3', '#C1DAD6', '#b7baba', '#ACD1E9', '#6D929B']


if __name__ == '__main__':    from __init__ import colour_map_earth_toneelse:    from . import colour_map_earth_tone

This way you can successfully import the var from __init__.py when both testing and production using.

original q: Can I use __init__.py to define global variables?