How to get the current running module path/name

前端 未结 8 1969
不知归路
不知归路 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-03 20:23

    The only way is to do path manipulation with os.getcwd(), os.path, file and whatnot, as you mentioned.

    Actually, it could be a good patch to implement for optparse / argparse (which currently replace "%prog" in the usage string with os.path.basename(sys.argv[0]) -- you are using optparse, right? -- ), i.e. another special string like %module.

提交回复
热议问题