How to get the File path of a Class in Python

avatar
Borislav Hadzhiev

Last updated: Feb 21, 2023
4 min

banner

# Table of Contents

  1. Get the file path of a class in Python
  2. If you only have access to an instance of the class
  3. Getting a relative path instead
  4. Get the file path of a class using os.path.abspath()

# Get the file path of a class in Python

Use the inspect.getfile() method to get the file path of a class.

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 the absolute file path 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 the relative file path of class # ๐Ÿ‘‡๏ธ another.py print(os.path.relpath(inspect.getfile(Employee)))

get file path of class

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

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__))

only having access to an instance of the 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.

# Getting a relative path instead

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 file path and returns a relative version of the path.

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

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

This is a three-step process:

  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

get file path of class using os path abspath

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

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev