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.
$ 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:
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
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:
Hence, to import the areaOfCirlce function from the import_function module, we can do the following:
Once imported, we can use the function as intended, as shown in the example code below:
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:
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:
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:
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:
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:
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:
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:
For example, to import the import_function module, we can run:
Once we import a module, we can access its function using the dot notation as shown in the syntax below:
For example, to call the say_hello() function, we can run:
The code above should return:
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:
An example is as shown:
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:
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:
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
- Importing a specific function from a module
- Importing a specific function from a module with an alias name.
- Importing an entire module and importing a module with an alternative name.
Thanks for reading, see you in the next ones.