How to Call a Function from Another File in Python

As a Python programmer, you may want to call a function from another file in Python. By doing so, you do not have to rewrite your code or copy-paste it into your existing code-base.

We will use Python 3.10.4 with Visual Studio Code as our code editor for this guide. But, of course, you are free to use the code editor of your choice.

Scripts, Functions, and Methods

Every time you write Python code, you are writing a script. These scripts are then executed in Python Shell, and you get the output.

On the other hand, functions are reusable codes that you can use when coding. This simplifies your approach to coding apps or websites.

Lastly, we have methods included in packages such as Numpy, Matplotlib, etc. These methods are pre-defined and provide the functions used on Python objects.

Learning exactly what scripts, functions, and methods mean can simplify the process of calling a function from another file in Python.

How to Call a Function from Another File in Python

Step 1: Create a script with functions

The first step is to create a script with functions. If you have already created a script, skip this part and check the second step.

For this tutorial, we will create file.py which contains four functions: addition(), subtract(), multiply() and Hello_world().

The code for it is as below.

#here we create a function that we want to call from another file
def addition(a,b):
#code for adding
return a +b

def Hello_World():
#Hi world!
return "Hello, World!"

def subtract(a,b):
if a > b:
return a - b
else:
return b - a

def multiply(a,b):
return a * b;

Step 2: Create the file you want to call the function

Next, we need to create another script where we call functions from the first file. Let’s name the new script file2.py

The code for file2.py is:

#import everything from file.py
from file import *

#storing Hello_world() return value to world variable
world = Hello_World()

#print world varaible to the screen
print(world)

The first line from file import * tells the compiler to import all the available functions in file.py. Next, we call Hello_World() function defined in file.py and print its value to the screen.

You can also import the functions by assigning them a variable and then accessing it through it.

from file as fl

And to access the functions, you need to use the following notation.

fl.Hello_World()

fl.addition()

Importing All Functions From Other File in Python
Importing All Functions Using import *

Step 3: Call selected functions

Similarly, you can call other functions in file2.py from file.py.

Let’s look at another example, but this time, we will import selected functions. Let’s name this script: file3.py

#importing selected functions from file.py
from file import addition, subtract

#using the addition function to add two numbers
add_result = addition(3,5)

#using the substract function to subtract two numbers
sub_result = subtract(5,3)

#printing out result
print(add_result)
print(sub_result)

To import selected functions, you need to separate them using a comma, as shown in the line from file import addition, subtract

The output is shown below.

Importing Selected Functions in Python
Importing Selected Functions in Python

 

Step 4: Things to keep in mind while calling functions

You can import multiple functions from different files as well.

If you are importing functions from a file that is not in the same folder, you need to use the exact path from the root directory.

So, if you have your file.py inside two directories, you need to import it using the following command.

from rootfolder.folder1.folder2.file import addition, subtract

That’s it! You have successfully learned how to call a function from another file in Python.

Was this article helpful?

Leave a Comment