Python - Call function from another file
Last Updated :
21 Aug, 2024
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:
- Create a Python file containing the required functions.
- Create another Python file and import the previous Python file into it.
- 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.
How to call functions from other Python file
Similar Reads
Calling a Python function from MATLAB We can call the Python functions and objects directly from MATLAB. To call Python functions from MATLAB, need to install a supported version of  Python. MATLAB supports versions 2.7, 3.6, and 3.7 MATLAB  loads Python when you type py.command. py.modulename.functionname Below examples shows how to ca
1 min read
How to import a class from another file in Python ? In this article, we will see How to import a class from another file in Python.Import in Python is analogous to #include header_file in C/C++. Python modules can get access to code from another module by importing the file/function using import. The import statement is that the most common way of in
4 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 f
3 min read
How to Define and Call a Function in Python In Python, defining and calling functions is simple and may greatly improve the readability and reusability of our code. In this article, we will explore How we can define and call a function.Example:Python# Defining a function def fun(): print("Welcome to GFG") # calling a function fun() Let's unde
3 min read
First Class functions in Python First-class function is a concept where functions are treated as first-class citizens. By treating functions as first-class citizens, Python allows you to write more abstract, reusable, and modular code. This means that functions in such languages are treated like any other variable. They can be pas
2 min read
How to import variables from another file in Python? To import variables from another file in Python, you need to use the import statement. By importing a file, you gain access to its variables and functions. This can be done by simply using import filename or from filename import variable_name to access the required variables or functions in your cur
4 min read