Import Class in Python

Import Class in Python

We will introduce different methods of importing a class from a file in Python.

Import Class in Python

We frequently encounter situations in programming where we must repeat specific processes. It is time-consuming to write code for them every time.

We employ the concept of Object-Oriented Programming (OOPs) to avoid such scenarios in which we use the program frequently whenever we need it while working in Python.

This article will demonstrate how to import a class from another file in Python using different methods.

The OOPs in Python

Object-oriented programming (OOPs) is a programming model in Python that employs objects and classes. Its goal is to use programming to create real-world concepts like inheritance, polymorphisms, and encapsulation.

The primary idea behind OOPs is to combine data and the algorithms that operate with it into a single unit, even though no other parts of the program may access it.

Now, let us go through an example in which we will develop a function to perform some operations, and we will try to import it from that file to our new file.

First, let us create a new file, Numsum, with a class performing some functions as shown below.

# python
class Operations:
    def __init__(self):
        self.sum_ = 0
    def To_sum(self,a,b):
        self.sum_ = a+b
        return self.sum_

We will import this file into another file where we want to use the class and its functions. Both files should be located in the same folder to make them easier to import.

Once we have imported the file, we will use the class operations and make a sum of 2 numbers, as shown below.

# python
import Operations from Numsum

obj = Numsum.Operations()

Sum = obj.To_sum(4,12)

print("Sum is :",Sum)


import class in python from same directory

From the above example, we easily imported the class operations and used its function to create a sum of 2 numbers.

Import Multiple Classes From Another File in Python

Now, we’ll look at how to import all of the classes from another file. It might be necessary to import all of the classes from other files.

We can read it using the command from file name import all. This indicates that we have imported all the classes from the supplied file.

We use the from file name> import * statement in this situation. This statement imports all classes from the file, as shown below.

# python
class Operations:
    def __init__(self):
        self.sum = 0

    def To_sum(self,a,b):
        self.sum = a + b
        return self.sum

class showData:
    def __init__(self):
        self.DATA= ""
    def show_data(self,INFO):
        self.INFO = INFO
        return self.INFO

Now we will import all the classes from our file and use them in a function. As shown below, we use * instead of a class name to import all the classes from a file.

# python
from NumSum import *

obj1 = Operations()


obj2 = showData()

print(obj2.show_data( "Importing Multiple classes from NumSum"))


import multiple classes in python from same directory

Import Class From Another Folder in Python

Now, suppose we want to import a class from a file located in another folder. Let’s understand it better with an example.

We will use the Operations class in, located in another folder, i.e., NewFolder. We must also specify the path of NewFolder.

For that, we will use the sys module. Take a look at the code below.

# python
import sys


from NewFolder.Numsum import Operations

obj = Operations()



import class in python from different directory

As you can see, we can also import classes from a file placed in some other folder using the Python module sys.

Rana Hasnain Khan avatar Rana Hasnain Khan avatar

Rana is a computer science graduate passionate about helping people to build and diagnose scalable web application problems and problems developers face across the full-stack.


Related Article - Python Class

  • Python Generator Class
  • Data Class Inheritance in Python
  • Serialize a Python Class Object to JSON
  • Python Abstract Property
  • Python Class Factory
  • Python Class Equality