Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to get "importing module" in "imported module" in Python?

Tags:

python

For imported module, is it possible to get the importing module (name)? I'm wondering if inspect can achieve it or not~

like image 992
Drake Guan Avatar asked Jul 11 '26 06:07

Drake Guan


2 Answers

It sounds like you solved your own problem: use the inspect module. I'd traverse up the stack until I found a frame where the current function was not __import__. But I bet if you told people why you want to do this, they'd tell you not to.

like image 171
John Zwinck Avatar answered Jul 14 '26 12:07

John Zwinck


Even if you got it to work, this is probably less useful than you think since subsequent imports only copy the existing reference instead of executing the module again.

like image 43
Ignacio Vazquez-Abrams Avatar answered Jul 14 '26 13:07

Ignacio Vazquez-Abrams