W3docs

How to retrieve a module's path?

You can use the __file__ attribute of a module to retrieve its path.

You can use the __file__ attribute of a module to retrieve its path. Here is an example code snippet:

Using the file attribute of a module to retrieve it's path

import mymodule
print(mymodule.__file__)

This will print the file path of the mymodule module.

You could also use the os.path module to retrieve the directory path of a module's file. Here is an example:

Using the os module to retrieve a module's path

import os
import mymodule
path = os.path.dirname(mymodule.__file__)
print(path)

This will print the directory path of the mymodule module.