How To Import Function From Another Python File

Spread the love

We need to import the function defined in another Python file from a Python file.

Import Function From Another Python File


def displayText():

We must call the method displayText() in some other Python file so that the text within this file is displayed whenever displayText() is called. Python modules can be used to do this.

  • Make a Python file with the necessary functions.
  • Make a new Python file and paste the contents of the old Python file into it.
  • Invoke the functions from the imported file.

In the following examples, the above strategy was used:

Example 1: The displayText() function is included in the Python file

# function

def displayText():

First of all create a file that name should be and then make a function as we show you above so, now the Python file is created, which calls the function displayText(). and then you can use the function where you want in the sublime file.

# importing all the

# functions defined in

from test import *

# calling functions



All of the functions defined in the file are imported into the above program, and then a function is called.

Expample 2: AddNumbers(), subractNumbers(), multiplyNumbers(), divideNumbers(), and modulusNumbers() are all included in the Python code ().


# functions

def addNumbers(a, b):
    print("Sum is ", a + b)
addNumbers(5, 8)
def subtractNumbers(a, b):
    print("Difference is ", a-b)
subtractNumbers(10, 5)
def multiplyNumbers(a, b):
    print("Product is ", a * b)
multiplyNumbers(2, 10)
def divideNumbers(a, b):
    print("Division is ", a / b)
divideNumbers(15, 3)
def modulusNumbers(a, b):
    print("Remainder is ", a % b)
modulusNumbers(10, 20)

Read More: How to use Python’s map() Function: Transforming Iterables?

Leave a Comment