'python how to get the program that has imported a module
I ran into some trouble with modules lately. I'm having a hard timefiguring out how I can get the file that has imported the module. basically:
import sys
imported = sys.get_modules_that_have_imported_this_script()
#and then the rest of the code
to be more specific. modules run when you import them right? so is there a way to get what program the module is running from? (preferably the path or file name)
edit: i want the file name so I can use the inspect module to get source code
edit2: I don't want to get the source of the module. I want to find the main program from a module imported within it
edit3: maybe this will help https://pasteboard.co/OMO6tQoTKALI.png
Solution 1:[1]
I found the answer in here How to get filename of the __main__ module in Python?
If you have two files main.py and module.py, this is how you can find the path of main.py from module.py
main.py
import module
module.function()
module.py
import __main__
main_filename = __main__.__file__
def function():
...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Edward |
