Is it possible to get “importing module” in “imported module” in Python?

前端 未结 4 2011
暖寄归人
暖寄归人 2021-01-15 11:52

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

4条回答
  •  礼貌的吻别
    2021-01-15 12:16

    import inspect
    result = filter(lambda v:inspect.ismodule(v), globals().values())
    #result is a collection of all imported modules in the file, the name of any of which can be easily got by .__name__
    #replace globals() with inspect.getmembers(wanted_module) if you want the result outside the wanted module
    

提交回复
热议问题