The Wayback Machine - http://web.archive.org/web/20240615065336/https://www.geeksforgeeks.org/python-call-function-from-another-file/
Open In App

Python – Call function from another file

Last Updated : 21 May, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Given a Python file, we need to call a function in it defined in any other Python file. 
Example: 
 

Suppose there is a file test.py which contains the definition of the function displayText(). 
#test.py>
def displayText(): 
    print( “Geeks 4 Geeks!”)
We need to call the function displayText() in any other Python file such that wherever we call displayText() function displays text present in it. This can be done using Python modules. 
 

Approach:
 

  1. Create a Python file containing the required functions.
  2. Create another Python file and import the previous Python file into it.
  3. Call the functions defined in the imported file.

The above approach has been used in the below examples:
Example 1: A Python file test.py is created and it contains the displayText() function.
 

Python3




# test.py>
 
# function
def displayText():
    print( "Geeks 4 Geeks !")


Now another Python file is created which calls the displayText() function defined in test.py.
 

Python3




# importing  all the
# functions defined in test.py
from test import *
 
 
# calling functions
displayText()


Output:
 

Geeks 4 Geeks!

In the above program, all the functions defined in test.py file is imported then a functions is called.
Example 2: A Python file calc.py is created containing addNumbers(), subractNumbers(), multiplyNumbers(), divideNumbers() and modulusNumbers().
 

Python3




# calc.py>
 
# functions
def addNumbers(a, b):
    print("Sum is ", a + b)
 
def subtractNumbers(a, b):
    print("Difference is ", a-b)
 
def multiplyNumbers(a, b):
    print("Product is ", a * b)
 
def divideNumbers(a, b):
    print("Division is ", a / b)
 
def modulusNumbers(a, b):
    print("Remainder is ", a % b)


The functions defined in calc.py is called in another Python file.
 

Python3




# importing limited functions
# defined in calc.py
from calc import addNumbers, multiplyNumbers
 
 
# calling functions
addNumbers(2, 5)
multiplyNumbers(5, 4)


Output:
 

7
20

In the above program, all the functions defined in calc.py are not imported. 
To import all the functions defined in a Python file:
Syntax: 
 

from file import *

To import only required functions defined in a Python file:
Syntax:
 

from file import func1, func2, func3

Example 3: 
The below Python files test.py and calc.py are created having various function definitions.
 

Python3




# test.py>
 
# function defined in test.py
def displayText():
    print("\nGeeks 4 Geeks !")


Python3




# calc.py>
 
# functions defined in calc.py
def addNumbers(a, b):
    print("Sum is ", a + b)
 
def subtractNumbers(a, b):
    print("Difference is ", a-b)
 
def multiplyNumbers(a, b):
    print("Product is ", a * b)
 
def divideNumbers(a, b):
    print("Division is ", a / b)
 
def modulusNumbers(a, b):
    print("Remainder is ", a % b)


Both files are imported into an another Python file named file.py.
 

Python3




# file.py>
 
# importing all the functions
# defined in calc.py
from calc import *
 
# importing required functions
# defined in test.py
from test import displayText
 
 
# calling functions defined
# in calc.py
addNumbers(25, 6)
subtractNumbers(25, 6)
multiplyNumbers(25, 6)
divideNumbers(25, 6)
modulusNumbers(25, 6)
 
# calling function defined
# in test.py
displayText()


Output:
 

Sum is  31
Difference is  19
Product is  150
Division is  4.166666666666667
Remainder is  1

Geeks 4 Geeks!

In the above program, functions defined in test.py and calc.py are called in a different file which is file.py.
 



Similar Reads

Python - Call function from another function
Prerequisite: Functions in Python In Python, any written function can be called by another function. Note that this could be the most elegant way of breaking a problem into chunks of small problems. In this article, we will learn how can we call a defined function from another function with the help of multiple examples. What is Calling a Function
4 min read
Is Python call by reference or call by value
Python utilizes a system, which is known as "Call by Object Reference" or "Call by assignment". If you pass arguments like whole numbers, strings, or tuples to a function, the passing is like a call-by-value because you can not change the value of the immutable objects being passed to the function. Passing mutable objects can be considered as call
5 min read
Call a Class Method From another Class in Python
In object-oriented programming, classes play a pivotal role in organizing and structuring code. Python, being an object-oriented language, allows the creation of classes and their methods to facilitate code modularity and reusability. One common scenario in programming is the need to call a method from one class within another class. In this articl
3 min read
Python - Copy contents of one file to another file
Given two text files, the task is to write a Python program to copy contents of the first file into the second file. The text files which are going to be used are second.txt and first.txt: Method #1: Using File handling to read and append We will open first.txt in 'r' mode and will read the contents of first.txt. After that, we will open second.txt
2 min read
Python program to reverse the content of a file and store it in another file
Given a text file. The task is to reverse as well as stores the content from an input file to an output file. This reversing can be performed in two types. Full reversing: In this type of reversing all the content gets reversed. Word to word reversing: In this kind of reversing the last word comes first and the first word goes to the last position.
2 min read
Python - Copy all the content of one file to another file in uppercase
In this article, we are going to write a Python program to copy all the content of one file to another file in uppercase. In order to solve this problem, let's see the definition of some important functions which will be used: open() - It is used to open a file in various modes like reading, write, append, both read and write.write() - It is used t
2 min read
How Can I Make One Python File Run Another File?
In Python programming, there often arises the need to execute one Python file from within another. This could be for modularity, reusability, or simply for the sake of organization. In this article, we will explore different approaches to achieve this task, each with its advantages and use cases. Making One Python File Run Another File in PythonBel
2 min read
Read content from one file and write it into another file
Prerequisite: Reading and Writing to text files in Python Python provides inbuilt functions for creating, writing, and reading files. Two types of files can be handled in python, normal text files and binary files (written in binary language,0s, and 1s). Text files: In this type of file, Each line of text is terminated with a special character call
2 min read
Decorator to print Function call details in Python
Decorators in Python are the design pattern that allows the users to add new functionalities to an existing object without the need to modify its structure. Decorators are generally called before defining a function the user wants to decorate. Example: # defining a decorator def hello_decorator(func): # inner1 is a Wrapper function in # which the a
3 min read
Call a function by a String name - Python
In this article, we will see how to call a function of a module by using its name (a string) in Python. Basically, we use a function of any module as a string, let's say, we want to use randint() function of a random module, which takes 2 parameters [Start, End] and generates a random value between start(inclusive) and end(inclusive). Here, we will
3 min read
Article Tags :
Practice Tags :