How to get the current running module path/name

前端 未结 8 1985
不知归路
不知归路 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:11

    I think you're actually looking for the __name__ special variable. From the Python documentation:

    Within a module, the module’s name (as a string) is available as the value of the global variable __name__.

    If you run a file directly, this name will __main__. However, if you're in a module (as in the case where you're using the -m flag, or any other import), it will be the complete name of the module.

提交回复
热议问题