In the article on Standard Operator Functions in Python, we came across a module named ‘operator’ which is used to perform basic arithmetic operations using predefined functions. In this article, let us explore more about Modules in Python.
What Are Python Modules?
Python modules are files containing executable statements that can be imported into your program to perform a specific task. A single module can have either a function or a class or a combination of both.
Before understanding them in detail, let us see how they help us in programming.
Why Do We Need Python Modules?
Consider a scenario where you have written 500 lines of code to build an application on a single file. After executing the code, assume the compiler has returned some errors. To fix them, you need to debug (removing errors) the code. Debugging 500 lines of code written on a single file is a tedious job. However, this can be done using Python modules.
Let us try to relate the above scenario with a simple example. Consider the below-given calculator program that performs these operations – addition and subtraction. Let us run the below code and see what it prints.
def add(a, b):
return a + b
def sub(a, b):
return a - b
res1 = add(20, 10)
res2 = sub(20, 10)
Traceback (most recent call last):
File "/home/f30735ee2813d92c912932dd4f412d1f.py", line 9, in
NameError: name 'res' is not defined
We got an error. Since this is a simple code, we can easily debug and fix the error. But this might become challenging in case of a large piece of code. So to make debugging easier, we can break the function definition and the main function by writing them down in separate files. These individual files are referred to as modules.
Python has some built-in modules which are pre-defined in its library. Also, a user can create his/her own module and import it wherever required. Next, let us see how to create our own modules.
How To Create Modules In Python?
To create a module, you need to install Python on your system. If you have already installed, go ahead and follow the below steps. If not, click here to install now.
Once Python is installed on your system, you can create your own module by simply using a Notepad file.
Step 1: Open Notepad.
Step 2: Write the function definition of the above code in one notepad file and save it on the desktop of your system with the name ‘calc.py’ as shown in the below image.
Step 3: Write the main function of the above code in another notepad file and save it on the desktop with the name ‘main.py’
Here, calc is a module that needs to be used inside the main function. So next, let us see how to use this module inside the main function.
How To Use Modules in Python?
To use a module inside the main function, you need to import it using the keyword ‘import’ at the beginning of the main function. This is like linking the module with our main() function.
Step 4: Include the below-given line in your ‘main.py’ file and save it.
from calc import *
By using the above line, we are instructing the Python interpreter to import all the functions defined in the module ‘calc.py’ to the main() function. If you need to import only add() function, then you can write as shown below.
from calc import add
Since we have imported our module into the main() function, it is enough if we execute only the main() function in the command prompt to obtain the result.
Step 5: To execute the main() function, open Command Prompt
Check the path of the command prompt. Since you have saved your module and main() function in Desktop, the command prompt path should also be until Desktop. If not, change the path by giving the command ‘cd Desktop’.
Now, execute the command ‘python main.py’. You will get the output as shown in the below image.
Go ahead and try creating your own modules.
Modules In Python FAQs
Python modules are files containing executable statements that can be imported into your program to do a specific task. A single module can have either a function or a class or a combination of both.
- Each and every module is independent unless linked explicitly. So, any changes made in one module will not affect another module.
- Modules provide the reusability of code. So, a single module can be reused in another application (if required) or stored for later purposes.
To use a module in your program, you need to import it using the module name followed by the keyword ‘import’.