In this article we will discuss about how to call a function from another file in python.
Before reading this, you must know about the function in python.
Python Function
In python, function are the block of instructions which are used to perform a specific task until the satisfaction of given condition. Python function can perform various tasks like evaluation, computation, etc. And only after being called a function executes. In python there are two types of function i.e. Built-in function and User-defined function.
How to call a function from another file in Python
In Python, We can call a function from another file by following a simple approach which is as follows;
• Firstly, we have to create a python file in which the required functions are added.
• Then, we have to create another file and import the previous file into the new python file.
• Then at last, call the function that are defined in imported python file.
Pre-requisite
If you want to import all the functions defined in a Python file:
Syntax:
from file import*
If you want to import only required or limited functions defined in a Python file:
Syntax:
from file import func1, func2, func3
Let's understand the above approach and syntax with the help of following examples;
Illustration no.1
A Python file named is created and it contains the function.
# demo.py># function
def foo():
print("Learn Programming with Answersjet")
Now create another python file and call the foo() function defined in demo.py
# importing# functions defined in demo.py
from demo import*
# calling functions
foo()
Output
Learn Programming with Answersjet
Illustration no.2
A new Python file named is created and it contains , , and functions.
# solutions.py># functions
def add(x, y):
print("Sum of two number is ", x + y)
def subtract(x, y):
print("Difference between two number is ", x-y)
def multiply(x, y):
print("Product of two number is ", x * y)
def divide(x, y):
print("Division by two number is ", x / y)
Now create another python file and call the limited functions ie.., , and function that are defined in solutions.py
# importing limited functions# defined in solutions.py
from solutions import add, subtract, multiply
# calling functions
add(2, 10)
subtract(5, 4)
multiply(5, 8)
Output
121
40
Above, all the defined functions are not imported in
Illustration no.3
The and python files are created with various function definition.
# demo.py# function
def foo():
print("\nLearn Programming with Answersjet")
# solutions.py># functions
def add(x, y):
print("Sum of two number is ", x + y)
def subtract(x, y):
print("Difference between two number is ", x-y)
def multiply(x, y):
print("Product of two number is ", x * y)
def divide(x, y):
print("Division by two number is ", x / y)
Import both the above files and into another python file named with
# file.py># importing all the functions
# defined in solutions.py
from solutions import*
# importing required functions
# defined in demo.py
from demo import foo
# calling functions
# in solutions.py
add(2, 6)
subtract(14, 6)
multiply(5, 6)
divide(100, 4)
# calling function
# in demo.py
foo()
Output
Sum of two number is 8Difference between two number is 8
Product of two number is 30
Division by two number is 25
Learn Programming with Answersjet
And above in the , the functions defined in and are called.
Conclusion
Above we have discussed about how to call a function from another file in python. In python, function are the block of instructions which are used to perform a specific task until the satisfaction of given condition. In Python, We can call a function from another file by following a simple approach of import.