In Python, is there a way to determine which module/package imported a downstream package? For example, imagine if we have package_A
, package_B
, and package_C
. In my scenario, both package_A
and package_B
depend on package_C
. So, if I import package_A
, package_C
automatically gets imported. Similarly, if I import package_B
, package_C
automatically gets imported.
Assuming that I can change/modify package_C
, is there a way to determine whether package_C
was imported by package_A
or package_B
or neither? In other words, I want to add code to package_C
that will detect whether the parent importer included package_A
, package_B
, or neither. Note that package_A
may not be directly importing package_C
(i.e., package_A
might import package_Q
, which imports package_C
).
I've tried looking at traceback
but I'm not able to make any progress.