How to get the current running module path/name

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

    When run with -m, sys.path[0] contains the full path to the module. You could use that to build the name.

    source: http://docs.python.org/using/cmdline.html#command-line

    Another option may be the __package__ built in variable which is available within modules.

提交回复
热议问题