(An Unofficial) Python Reference Wiki

"clearly marked experimental"

The __file__ attribute

The __file__ module attribute contains the pathname of the file from which the module was loaded, if it was loaded from a file.

The __file__ attribute is not present for C modules that are statically linked into the interpreter; for extension modules loaded dynamically from a shared library, it is the pathname of the shared library file.