Python

How to Call a Function From Another File Python

Python is a high-level interpreted language that provides developers with easy-to-learn syntax. In addition, Python delivers robust tools and utilities for building highly scalable and complex applications despite its simplistic nature.

Python allows us to implement modular programming where we can define standalone logic and import it into other parts of the program.

In this tutorial, we will learn how you can get started in Python modular programming by defining and calling functions from different files?

What is a Python Module?

In Python, a module refers to a python file containing source code that can be imported into other programs.

Hence, any file that ends in .py extension and has python code can be considered a python module.

Declare a Python Function

To understand how to import and call a function from another file in Python, let us start by defining that function.

We recommend creating a sample workspace where you can store your python code.

$ mkdir python-workspace
$ cd python-workspace
$ touch import_function.py

In the above example, we create a python file called import_function.py. Feel free to rename this file to any value you wish.

Inside the import_function.py file, define a function that calculates a circle’s area given a specific radius.

An example definition is shown below:

def areaOfCircle(radius, PI):
    return radius * radius * PI;

Python Import Function

Once we have our directory structure created a sample file with our function definition, we can proceed to import the function and call it from another file.

Create a new python file in the same directory and name it main.py

$ touch main.py

In this file, we will import the areaOfCircle function and call it to calculate the area of a circle.

There are various ways we can import this function. Let us discuss.

Python Import Specific Function

Python allows us to import a specific function from a module. Although this may seem pointless when you have a single function, it can be beneficial compared to importing all the functions within a module.

To import a specific function in Python, we use the syntax shown below:

from module_name import function_name

Hence, to import the areaOfCirlce function from the import_function module, we can do the following:

from import_function import areaOfCircle

Once imported, we can use the function as intended, as shown in the example code below:

import math
from import_function import areaOfCircle

print(f"Area: {areaOfCircle(7, math.pi)} cm2")

In the above code, we call the areaOfCicle function with a radius of 7 and the Python PI constant.

Running the code above should return the output as shown:

$ Python main.py
Area: 153.93804002589985 cm2

Python Import Specific Function With Alternate Name

We may need to rename the function we are importing in some instances. This could be due to readability reasons or avoid name collisions in our program.

We can do this using the ‘as’ keyword. The ‘as’ keyword is followed by the alternative name of the function we are importing.

For example, to import the areaOfCircle function without renaming it in the source file, we can do the following:

from import_function import areaOfCirlce as circle;

In this case, we are renaming the function to ‘circle.’

NOTE: Renaming the function using the ‘as’ keyword does not alter the function definition.

We can now call the function with the new name as:

import math
from import_function import areaOfCircle as circle

print(f"Area: {circle(7, math.pi)} cm2")

The above code should return the same output, given the same input as shown below:

$ Python main.py
Area: 153.93804002589985 cm2

Python Import Multiple Specific Functions

Suppose we want to import more than one function from a module? We can do this by separating them via a comma.

To illustrate, go to the import_function.py file and add a new function called say_hello. An example code is as shown:

def say_hello():
    print("Hi")

Now we have two functions in the import_function module, one that returns the area of a circle and one that prints ‘Hi.’

To import and use these functions in the main.py file, add the line:

from import_function import areaOfCircle, say_hello

Note how we separate each function via a comma.

Python Import Module

At other times, you may need to import an entire module. For that, you can use the import keyword followed by the module name:

The syntax is as shown:

import module_name

For example, to import the import_function module, we can run:

import import_function;

Once we import a module, we can access its function using the dot notation as shown in the syntax below:

module_name.function_name();

For example, to call the say_hello() function, we can run:

import_function.say_hello()

The code above should return:

$ Python main.py
Hi

Python Import Module With an Alternative Name

We can give an imported module and alternative name using the keyword like a function import.

The syntax is as shown:

import module_name as new_name;

An example is as shown:

import import_function as i_func;

In this case, we import the import_function module and give it an alternative name as i_func.

We can then call the functions from the module using its alias name:

i_func.say_hello()

Python Import All Functions

Suppose you do not want the entire module but are only interested in the specific module. However, the modules could be a lot, thus making the comma-separated function import unsuitable.

We can do this by specifying an asterisk as the function name.

The syntax is as shown below:

from module_name import *

The asterisk tells Python to import all the functions from that module.

Conclusion

In this article, we explored various methods of importing modules in functions. For example, we covered

  1. Importing a specific function from a module
  2. Importing a specific function from a module with an alias name.
  3. Importing an entire module and importing a module with an alternative name.

Thanks for reading, see you in the next ones.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list