If you invoke python foo.py or python -m foo, is there a way to detect, from inside Python, that the file foo.py / the module foo was used to run the program?
The docs for sys.argv suggest that sys.argv[0] can be used for this purpose, but it only works part-way. In my testing, invoking python -m foo still populates sys.argv[0] with the full filename of foo.py, not the module name foo, so it doesn't do what I would want it to do in all cases. I believe that this is also equivalent to __file__. It is very error-prone to try and extract a fully qualified module name from a filename.
I want to be able to distinguish between cases like python a/b/c.py and python -m a.b.c, as well as invoking Python with a "wrapper" like python -m unittest a.b.c. I do not want to rely on heuristics on sys.argv[0], if at all possible.
In reply to the comments and close votes: I am not interested in workarounds; I have some already. I am asking this question because it want to know if this very specific thing is possible, not because I want help with my work in general.