How to get the current running module path/name
This works for me:
__loader__.fullname
Also if I do python -m b.c from a\ I get 'b.c' as expected.
Not entirely sure what the __loader__ attribute is so let me know if this is no good.
edit: It comes from PEP 302: http://www.python.org/dev/peps/pep-0302/
Interesting snippets from the link:
The load_module() method has a few responsibilities that it must fulfill before it runs any code:
...
- It should add an __loader__ attribute to the module, set to the loader object. This is mostly for introspection, but can be used for importer-specific extras, for example getting data associated with an importer.
So it looks like it should work fine in all cases.
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.