Working With Modules
Before proceeding to learn classes in Python, we would like to discuss modules and how to import modules in our Python Program.
What is a module?
A module is a Python script file, that consists of functions, classes, and data (variables or immutable data). At, current moment forget about classes and just focus on functions and data, that we have already discussed.
Why do we need modules?
It is cumbersome to write a large number of lines of codes in a single file. Also, when we like to use functions written by other programmers we need to import their files. In order to group related code and data, we simply write related codes in separate Python script.
Based on our current Python script requirement, we simply import individual functions or complete modules (Python script file), to use in our program.
How to import the module in our Python Script?
We would like to explain things to you from scratch. So, instead of importing the existing module, we will build a module/Python script and then we will import in another Python Program or Script.
Creating a calculator.py Module
The below code is an aggregation of four different functions, that provide basic operations of a calculator such as addition, multiplication, subtraction, division
Code:
def add(a, b):
c = a + b
return c;
def sub(a, b):
c = a - b
return c;
def mul(a, b):
c = a * b
return c;
def div(a, b):
c = a / b
return c;
How to import module in a Python Script/Program
- In order to import a Python module, the module must be placed in the current directory or the default path where python is installed.
- Now there are two methods to access the functions in the module with or without a dot. operator.
Without Using dot operator
- If you want to import all the functions from calculator.py in your current program use
- from calculator import *
- If you want to import specific function from caluclator.py in your current program use
- from calculator import add
Code:
#import all the functions from calculator.py from calculator import * print(add(3, 4)) print(mul(3, 4)) print(sub(3, 4)) print(div(3, 4))
Output:
7 12 -1 0.75
Code:
#import specific function from caluclator.py from calculator import add print(add(91, 909)) #We will get error if we will use any other function print(mul(91, 909))
Output:
1000
Traceback (most recent call last):
File "practise.py", line 7, in <module>
print(mul(91, 909))
NameError: name 'mul' is not defined
Using dot operator
- If you want to import all the functions from calculator.py in your current program use
- import calculator
Code:
#import all function from caluclator.py import calculator #We need to access function using the dot operator print(calculator.add(91, 909)) #We need to access function using the dot operator print(calculator.mul(91, 909))
Output:
1000 82719
Difference b/w Modules and Packages
Ideally, we access the function using a dot operator. The scenario which we discussed above makes you understand how to import a single module. In practical, we deal with a package and not an individual Python file. The package is a collection of Python module or Scripts (.py) whereas a module is a single Python Script (.py). A package is a directory of different Python modules/scripts containing an additional __init__.py file, which is needed to distinguish a package from a directory that only consists of Python scripts.
So, you must be excited to build your own Python package, which consists of different modules and an additional __init__.py file. We advise you to learn the concept of classes in the next section, after that we will learn how to build a package.