How to get the current running module path/name

前端 未结 8 1968
不知归路
不知归路 2021-02-03 19:19

I\'ve searched and this seems to be a simple question without a simple answer.

I have the file a/b/c.py which would be called with python -m a.b.c

8条回答
  •  鱼传尺愫
    2021-02-03 20:18

    Why does nobody mentioned the .__module__?

    When doing a self.__module__ you will get the module path. You can also do this outside of the class:

    Class A:
       self.__module__  # gets module.filename
    
    def get_module():
        A.__module__ # also gets module.filename
    

提交回复
热议问题