get the path of a module in python

import a_module
print(a_module.__file__)

Here is what the above code is Doing:
1. It’s importing the module a_module.
2. It’s printing the path to the module’s file.
3. It’s printing the path to the module’s __init__.py file.
4. It’s printing the path to the module’s __main__.py file.
5. It’s printing the path to the module’s __init__.pyc file.

The __file__ attribute is only available if the module is run as a script.

If you try to access the __file__ attribute from a module that’s being imported, you’ll get an AttributeError.