Python modules and packages

Python module

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:

 import <module_name>

For example

import math
import random

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

  1. sin value of 30 degrees i.e. sin(30) ,
  2. cosine value of 30 degrees i.e. cos(30),
  3. 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.

Trigonometric formulae
Trigonometric formulae


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

Example

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))

Output:

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.

Syntax:

round(number, number_to_round_upto)

Example:

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  

Example:

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)

Output:

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

Syntax:

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())

Output:

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())

Output:

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

Python package directory structure
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:

Support available

We can also use __init__.py file to make import easier. __init__.py is used to import modules directly via package name

for example 

__init__.py file

from support import *

then we can do like this in client.py file

import package_example

package_example.support()

Output:

Support available

Making use of __init__.py for importing makes our import look cleaner and more organized.

Leave a Reply

Your email address will not be published. Required fields are marked *