I learn that python will make sure to input each package only once by appending the __name__ of each imported (either directly or indirectly ) package to sys.path only once.
I'm currently working with monkey patch in python, and I want to make sure I can always make the right change.
Say there's a function in a multi-layer package packageA.subB.subC.moduleFoo.func, I have a file named patch.py, which implements a monkey patch function to modify func:
import packageA.subB.subC.moduleFoo
def new_func():
pass
def modifier():
packageA.subB.subC.moduleFoo.func = newfunc
In my current script, I import packageA.subB.subC.moduleFoo as well as file patch.py. I want to use the modifier function in the current python script.
I'm wondering whether as long as I correctly access func in both the current script and patch.py, my modifier function will work properly. If the answer is yes, what will be stored in sys.path in the main script to help make this all right?
(Note that there're multiple ways to import a function from a multi-layer package, for example:
import packageA.subB.subC.moduleFoo # access with packageA.subB.subC.moduleFoo.func
from packageA.subB.subC import moduleFoo # access with moduleFoo.func
from packageA.subB.subC.moduleFoo import func