SOLVED: Calling a function from another file in Python

Different methods for calling a function from another file in Python

When we wish to call a function from another file in Python, we have different scenarios to achieve our goal.

  • Calling a function from another file
  • Calling a function containing arguments from another python file
  • Calling a function present in a file with a different directory
  • Importing all functions from another Python file
  • Calling a function without using the import function

 

Example-1: Calling a function from another file

In this scenario, we are calling a function from another file. Let us take a compute.py file having a function interest to compute the simple interest of given principal and duration. We will then write a demo.py file that has saving function, which when called makes call to interest function to compute simple interest.
compute.py

Advertisement
# Function to compute simple interest at fixed rate of 5%
def interest():
    p=int(input("Enter the principal amount"))
    n=int(input("Enter the number of years"))    
    print("Computing simple interest at rate of 5%")
    r=5
    si=(p*r*n)/100
    return si

demo.py

from compute import interest

def saving():
    print("Interest accrued is",interest())

saving()

Output

Enter the principal amount
1000
Enter the number of years
2
Computing simple interest at rate of 5%
Interest accrued is 100.0

 

Example-2: Calling Function containing arguments from another python file

In this scenario, we are calling a function from another file but with the arguments.  Let us firstly write two python files compute.py and demo.py. We will write a function interest to compute simple interest when we pass amount and number of years. The rate is fixed as 5%. Hence, the function will compute simple interest and return the amount to the calling function.
compute.py

# Function to compute simple interest at fixed rate of 5%
def interest(p,n):
    print("Computing simple interest at rate of 5%")
    r=5
    si=(p*r*n)/100
    return si

demo.py

from compute import interest
# Get input from the user
p=int(input("Enter the principal amount"))
n=int(input("Enter the number of years"))
# Calling a function of compute.py file with p and n as an  arguments
print("Interest accrued for the principal",p, "for",n, "years at the rate of 5% is",interest(p,n))

Output

Enter the principal amount
1000
Enter the number of years
2
Computing simple interest at rate of 5%
Interest accrued for the principal 1000 for 2 years at the rate of 5% is 100.0

 

Example-3: Calling function present in a file with a different directory

In this scenario, we are calling a function from another file in different directory. Let us save the file compute.py inside the folder bank. Whereas, demo.py is saved outside the folder bank. Hence, we are accessing the file from the module bank stored in the different directory. In this case, only the import statement requires the modification. The way function is called remains same.

Advertisement

bank/compute.py

# Function to compute simple interest at fixed rate of 5%
def interest(p,n):
    print("Computing simple interest at rate of 5%")
    r=5
    si=(p*r*n)/100
    return si

demo.py

from bank.compute import interest
# Get input from the user
p=int(input("Enter the principal amount"))
n=int(input("Enter the number of years"))
# Calling a function of compute.py file with p and n as an  arguments
print("Interest accrued for the principal",p, "for",n, "years at the rate of 5% is",interest(p,n))

Output

Enter the principal amount
1000
Enter the number of years
2
Computing simple interest at rate of 5%
Interest accrued for the principal 1000 for 2 years at the rate of 5% is 100.0

 

Example-4: Importing all functions from another Python file

In this scenario, Let us take the compute.py file but with one more function added to compute compound interest. Here, we will import all the functions present in compute.py file. The compute.py contains two functions to compute simple interest and compound interest. We will then make a call to a function demo.py file.

compute.py

# Function to compute simple interest at fixed rate of 5%
def interest(p,n):
    print("Computing simple interest at rate of 5%")
    r=5
    si=(p*r*n)/100
    return si
 
# Function to compute Compound interest at fixed rate of 5% 
def compoundinterest(p,n):
    print("Computing Compound interest at rate of 5%")
    r=5
    ci=p*pow(1+r/100,n)-p
    return ci

demo.py

from compute import *
# Get input from the user
p=int(input("Enter the principal amount"))
n=int(input("Enter the number of years"))
# Calling a function of compute.py file with p and n as an  arguments
print("Simple Interest accrued for the principal",p, "for",n, "years at the rate of 5% is",interest(p,n))
print("Compound Interest accrued for the principal",p, "for",n, "years at the rate of 5% is",compoundinterest(p,n))

Output

Enter the principal amount
1000
Enter the number of years
5
Computing simple interest at rate of 5%
Simple Interest accrued for the principal 1000 for 5 years at the rate of 5% is 250.0
Computing Compound interest at rate of 5%
Compound Interest accrued for the principal 1000 for 5 years at the rate of 5% is 276.2815625000003

 

Example-5: Call a function without using the import statement

In this scenario, we will not use import statement, rather use importlib and use the function from the file compute.py . Let us assume, our project structure is something like as shown below.
computeproject->computelib->compute.py
computeproject->demo.py

Advertisement

compute.py

# Function to compute simple interest at fixed rate of 5%
def interest(p,n):
    print("Computing simple interest at rate of 5%")
    r=5
    si=(p*r*n)/100
	print("Simple Interest accured for the principal",p, "for",n, "years at the rate of 5% is",si)

demo.py

import importlib
from inspect import isfunction

def call_func(full_module_name, func_name, *argv):
    module = importlib.import_module(full_module_name)
    for attribute_name in dir(module):
        attribute = getattr(module, attribute_name)
        if isfunction(attribute) and attribute_name == func_name:
            attribute(*argv)

call_func('compute', 'interest', 1000,5)

Output

Computing simple interest at rate of 5%
Interest accured for the principal 1000 for 5 years at the rate of 5% is 250.0

 

Summary

The knowledge of Calling a function from another file in Python is very useful while working on real time applications. It helps in re-usability and data abstraction. In many situations, we will need to call a function that is used in many other files of a project and is lying in some common functionality file. In this tutorial, we covered the different scenario to call a function lying in another file and directory with an example. All in all, this tutorial, covers everything that you need to know in order to understand calling a function from another file in Python.

 

References

Importlib

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment

X