How to import a class from another file in Python

In this tutorial, you will learn how you can import one class of python program to another file of python program.

In python programming you can use import keyword to make class in one file available in another. Importing class from another file using import keyword allowing you to reuse code while keeping your project maintainable.

Import a class from another file in python

Let suppose you have two file called and in directory and you want to reuse the class in then you can do this with the help of import keyword.

class Student:
	def init(self, name, roll_no):
	def get_detail(self): + ‘ ’ + str(self.roll_no)

now let use the student class in

from student import Student
stud_detail =Student(“roy”, 71)

Output is:

roy 71

Was this article helpful?

1 thought on “How to import a class from another file in Python”

Leave a Comment