How To Import Function From Another Python File

Share This Post

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?


Related Posts

Python for Loop Iteration: How to Easily Manipulate a List

One of the most powerful features of the Python...

How To Get First Characters of a String in Python

Way to find the first characters of a string...

How To Convert Tuple To String In Python

In this post, you will learn how to convert...

How To Convert String to Double in Python

Good to hear, that you want to convert a...

Python TypeError: String Index Out Of Range Solution

A string is an array of characters and part...

How To Remove Empty Strings From a List Of Strings

On this page, we will discuss how to remove...
- Advertisement -spot_img