How can I access the current executing module or class name in Python?

前端 未结 11 1849
醉酒成梦
醉酒成梦 2020-12-05 01:30

I would like to be able to dynamically retrieve the current executing module or class name from within an imported module. Here is some code:

foo.py:

11条回答
  •  清歌不尽
    2020-12-05 02:31

    To get the current file module, containing folder, here is what worked for me:

     import os
     parts = os.path.splitext(__name__)
     module_name = parts[len(parts) - 2]
    

提交回复
热议问题