Python Import Class From Another File with Examples

In this article, we are going to discuss some methods used on how python import class from another file.

Why Do We Need To Import A Class From Another File In Python?

We need to Import class from another file in python because it allows us to use them within the current program.

Apart from this, it helps improve the readability and reusability of code. Importing class from another file in python can be done within the same or from different folders.

How To Import Class From Another Python File?

In this section, we will discuss the step-by-step process on how to import class from another python folder. So, let’s begin…

Here, we will create a class named “PFF“, which the class has two methods: “add()” and “sub()“.

Aside from that, an accurate function is created named “method()” in the same file in python.

This file will act as a module for the main python file.

Let’s name the file of python code above as “module.py

Here’s the code:

class PFF:
      
    # methods
    def add(self, a, b):
        return a + b
    def sub(self, a, b):
        return a - b
  
# explicit function      
def method():
    print("Python For Free")

Import

Next, it’s time to import the module and start trying out our new class and functions.

Here, we will import a module named “module” and create the object of the class named “PFF” inside of that module.

Also read: Perceptron Algorithm Python Implementation with Examples

Now, we will use its methods and variables.

Here’s the code:

import module
   
# Created a class object
object = module.PFF()
   
# Calling and printing class methods
print(object.add(20,10))
print(object.sub(20,10))
   
# Calling the function
module.method()

When you execute the program this will be the output:

30
10
Python For Free

Importing the module as we mentioned above, will automatically bring over every single class and perform within the module into the namespace.

If you only want to use one function, you can avoid confusing the namespace by only importing that function. We’ll show you how to do this in the program below: 

Here’s the code:

# import module
from module import method
   
# call method from that module  
method()

When you execute the program this will be the output:

Python For Free

In this process, we use class to import from another document case in python.

Import Multiple Classes From One File In Python

In this section, we will demonstrate how to import multiple classes in Python.

Here’s the code:

class PFF:

    # methods
    def add(self, a, b):
        return a + b

    def sub(self, a, b):
        return a - b


# explicit function
def method():
    print("Python For Free")


import module

# Created a class object
object = module.PFF()

# Calling and printing class methods
print(object.add(20, 10))
print(object.sub(20, 10))

# Calling the function
module.method()

When you execute the program the output is the same from the example above.

30
10
Python For Free

Conclusion

So, today we have completely discussed the step-by-step process of How To Import Class From Another File by providing example programs. Also, we have a demo on how to import multiple classes in one file in Python.

I hope this article helps you a lot to build a better and easy to understand python program.

Recommendation

By the way, if you want more python tutorials, We have here the list of different python tutorials that can fix your problem.

Inquiries

If you have any questions or suggestions about this article, please feel free to comment below, Thank You!

Leave a Comment