A module is a file containing Python definitions and statements. It is similar to a code library that you want to import (or use) in your application. A module is any code that resides in a python file(having .py extension). There is a lot of useful modules already available in python. Some of the modules don’t need to import such as print() statement, whereas another module like math, random, etc. should be imported to use in the application.
Importing a module
Importing a module is defined as making the module available in the application(in your case a python file). You should use the import statement to import the file. The syntax for import is given below:
Using a module in python
Once the module has been imported into your application, you can call the function or other variable, and perform the desired action.
Let’s see an example of using math module to calculate
- sin value of 30 degrees i.e. sin(30) ,
- cosine value of 30 degrees i.e. cos(30),
- tan value of 30 degrees ie. tan(30)
In python math.sin(x) function parameter x is in radian, we need to pass 30 degrees. Therefore we convert 30 degrees to radian and pass to the math method.
Let’s look at a tirgonometric ratio table and verify the result.
We would also like to calculate ceil value of a number (it is the top value of integer if we pass in a decimal value i.e. 4.4 = 5 in ceil value floor value( the lower value of the integer if we pass a decimal value, i.e. 4.4 = The square root of a number and the power of a number like x, y
import math a = 4.4 print("math.sin(30) = ", math.sin(math.radians(30))) # calculates sin(30) print("math.cos(30) = ", math.cos(math.radians(30))) print("math.tan(30) = ", math.tan(math.radians(30))) print("math.ceil(a) = ", math.ceil(a)) print("math.floor(a) = ", math.floor(a)) print("square root of 16 = ", math.sqrt(16)) print("5 to the power 3= ", math.pow(5, 3))
math.sin(30) = 0.49999999999999994 math.cos(30) = 0.8660254037844387 math.tan(30) = 0.5773502691896257 math.ceil(a) = 5 math.floor(a) = 4 square root of 16 = 4.0 5 to the power 3= 125.0
Here as you can see the output of math.sin(30) generates a number that is nearly equal to 0.5, if we want to get the value up to N(such as 1,2, 3,4 or 5 and so on) decimal place we can use the round() method to trim the values.
if we do
print("math.sin(30) = ", round(math.sin(math.radians(30)), 3))
We get output like this
math.sin(30) = 0.5
Let’s import another module name random and generate a random number. that generate random number between 0 and 1
For generating random number between 0-1 we use random.random() method and for generating random numbers between two integers we use random.randint() method as shown below:
import random random_number = random.random() print("random number between 0-1 = ", random_number) random_int = random.randint(2,25) print("random number between 2-25=", random_int)
The output of yours may vary with mine because we are generating random number
random number between 0-1 = 0.5359248735051431 random number between 2-25= 12
Renaming imported module
You can rename the imported module and use them in your program using as keyword
import module_name as rename_name
For example we can rename random module as r and use r in place or random.
import random as r print("random number = ", r.random())
random number = 0.9848216281721253
Importing specific function from a module
We can also import function directly in our program from a module to do that we use from keyword
The syntax looks like this:
from <module-name> import <function,class,or variable name>
e.g. Let’s see the random function directly imported instead of module
from random import random print("random number = ", random())
random number = 0.3303874626111051
Packages in python
Packages are collection of modules bundled in a folder. It includes a special file named as __init__.py . The difference between module and a package is, module is single python file whereas a package consist of multiple modules inside a folder. It can have sub folder as well but all the sub-folders have special python file __init__.py .
To see an example lets create a support.py file inside a folder and create client.py in the same directory.
support.py file will have support() function which client.py file will use to execute some code
Python package directory structure
support.py file has following code
def support(): print("Support available")
client.py file calls the support function. The code inside client.py looks like this
from support import support support()
The __init__.py file is empty. We include __init__.py file to separate a python package from a directory. If the folder has __init__.py file, python treats it as a package, if it does not then it is treated as normal directory.
If we run client.py file we get the following output:
We can also use __init__.py file to make import easier. __init__.py is used to import modules directly via package name
from support import *
then we can do like this in client.py file
import package_example package_example.support()
Making use of __init__.py for importing makes our import look cleaner and more organized.