How do you determine the name of the importing module within the module that is being imported. I have the partial solution, but not the complete one.
The code is: A.py
import B
if __name__ == '__main__':
print 'This a test'
The B.py
import sys
import C
if sys.argv[0] == 'A':
doSomething()
At this point, I'm all set because within module B, I know that name of the main that invoked the importing which in this case is A. However, within B, an import of C is requested, and it is in C that I want to know whether B imported C? How is this done?
sys.argv[0] is not a name of module when import was performed. This is name of executable file.
On the other side, inside Python module __name__ equals to a) module name if it's executed by importing, b) "__main__" if it was executed as script.
Module doesn't "know" who performed import (no "parent" attribute or something like this). Define your behavior with different functions and call them from different modules.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With