Get the filepath of a class in Python

avatar

Borislav Hadzhiev

Last updated: Sep 13, 2022

banner

Photo from Unsplash

Get the filepath of a class in Python #

Use the inspect.getfile() method to get the filepath of a class, e.g. print(inspect.getfile(Employee)). The getfile() method takes an object and returns the name of the file in which the object was defined.

main.py
from another import Employee import inspect import os # ✅ get absolute filepath of class # 👇️ /home/borislav/Desktop/bobbyhadz_python/another.py print(inspect.getfile(Employee)) emp1 = Employee() # 👇️ /home/borislav/Desktop/bobbyhadz_python/another.py print(inspect.getfile(emp1.__class__)) # ----------------------------------------------- # ✅ get relative filepath of class # 👇️ another.py print(os.path.relpath(inspect.getfile(Employee)))

The example above assumes that you have an Employee class in a file called another.py located in the same directory.

another.py
class Employee(): pass

The inspect.getfile method returns the name of the file in which an object was defined.

main.py
from another import Employee import inspect # 👇️ /home/borislav/Desktop/bobbyhadz_python/another.py print(inspect.getfile(Employee))

The getfile() method throws a TypeError if the object is a built-in module, class or function.

If you only have access to an instance of the class, access its __class__ attribute in the call to the getfile() method.

main.py
from another import Employee import inspect emp1 = Employee() # 👇️ /home/borislav/Desktop/bobbyhadz_python/another.py print(inspect.getfile(emp1.__class__))

You can also use the type() class to achieve the same result.

main.py
from another import Employee import inspect # 👇️ /home/borislav/Desktop/bobbyhadz_python/another.py print(inspect.getfile(Employee)) emp1 = Employee() # 👇️ /home/borislav/Desktop/bobbyhadz_python/another.py print(inspect.getfile(type(emp1)))

The type class returns the type of an object.

Most commonly the return value is the same as accessing the __class__ attribute on the object.

The inspect.getfile() method returns the absolute path to the module in which the class is defined.

If you need to get a relative path, use the os.path.relpath() method.

main.py
from another import Employee import inspect import os # 👇️ another.py print(os.path.relpath(inspect.getfile(Employee)))

The os.path.relpath method takes a filepath and returns a relative version of the path.

Alternatively, you can use the os.path.abspath() method.

Get the filepath of a class using os.path.abspath() #

To get the filepath of a class:

  1. Use the sys.modules dictionary to get the module in which the class was defined.
  2. Use the __file__ attribute to get the pathname of the file from which the module was loaded.
  3. Use the os.path.abspath() method to get a normalized, absolute version of the path.
main.py
import os import sys from another import Employee result = os.path.abspath(sys.modules[Employee.__module__].__file__) # 👇️ /home/borislav/Desktop/bobbyhadz_python/another.py print(result) emp1 = Employee() result = os.path.abspath(sys.modules[type(emp1).__module__].__file__) print(result) # 👉️ /home/borislav/Desktop/bobbyhadz_python/another.py

The os.path.abspath method takes a path and returns a normalized, absolute version of the path.

Sys.modules is a dictionary that maps module names to modules that have already been loaded.

We used the __module__ attribute on the class to get the name of the module in which the class was defined.

main.py
import os import sys from another import Employee print(Employee.__module__) # 👉️ another # 👇️ /home/borislav/Desktop/bobbyhadz_python/another.py print(sys.modules[Employee.__module__].__file__)

The __file__ attribute on the module returns the pathname of the file from which the module was loaded.

Here is the complete code snippet.

main.py
import os import sys from another import Employee result = os.path.abspath(sys.modules[Employee.__module__].__file__) # 👇️ /home/borislav/Desktop/bobbyhadz_python/another.py print(result) emp1 = Employee() result = os.path.abspath(sys.modules[type(emp1).__module__].__file__) print(result) # 👉️ /home/borislav/Desktop/bobbyhadz_python/another.py
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.