How to develop a Python Module?


A file containing Python commands and definitions is referred to as a module. These files named .py which contain that contains Python code, such as example.py, and the name of the module is an example. Modules are used to divide down huge applications into smaller, more manageable files.

The prerequisites for using modules you should have Python 3 installed and a programming environment set up. If you don't already have one, you can refer to the installation and setup recommendations for a local programming environment.

Example 1

Let us look at an example to create a simple python module. Let us make a python file ad.py

def add(x, y): return (x+y)

After doing so, upon saving the file as ad.py, a module named ad.py is created.

In Python, we can import definitions from one module into another or into the interactive interpreter. To accomplish this, we employ the import keyword as shown below.

import module

Let us look at an example on how to import a python module. In the Python prompt, type the following to import our previously defined module ad.

import ad
#importing module ad.py

The below code can be referred to for understanding how to access functions developed in a custom module in python.

import ad
print(ad.add(9, 2))

Output

The output generated for the above line of code, when the add function from the user-defined ad module has been called can be seen below.

11

Example 2

Let us another example to develop a python module. Create a file called fibonacci.py and enter the following code in it −

def fib(n): # write Fibonacci series up to n a, b = 0, 1 while b < n: print(b, end=' ') a, b = b, a+b print() def fib2(n): # return Fibonacci series up to n result = [] a, b = 0, 1 while b < n: result.append(b) a, b = b, a+b return result

Now open your terminal and use cd command to change the directory containing this file and open the Python shell. Enter the following statements −

>>> import fibonacci
>>> fibonacci.fib(1000)
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
>>> fibonacci.fib2(100)
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]

Standard Python Modules

Python's standard library is extensive, providing a wide range of facilities, as evidenced by the lengthy table of contents listed below.

The library includes built-in modules (written in C) that provide access to system functionality such as file I/O that would otherwise be inaccessible to Python programmers, as well as Python modules that provide standardized solutions to many problems encountered in everyday programming. Examples of python standard modules are, math, queue, venv and zipfile.

Some of these modules are explicitly designed to encourage and improve Python program portability by abstracting away platform-specifics into platform-neutral APIs.

Example

We can use the import statement to import a module and the dot operator to access the definitions within it. Here's an example.

#python program to import math module import math print("Pi value is:", math.pi)

Output

The output generated is as follows.

Pi value is: 3.141592653589793

Renaming the imported Python Modules

Modules provide greater flexibility in code organization. This allows for a more concise and contextual name. In the, for example, we can import numpy as np. This makes our code more readable, but it also allows different modules to export members with the same name.

Syntax

import module as xyx

NumPy is a Python library for manipulating arrays. It also includes functions for working with linear algebra, the Fourier transform, and matrices. Travis Oliphant created NumPy in 2005.

It is an open-source project that you are free to use. NumPy is an abbreviation for Numerical Python. NumPy strives to provide array objects that are up to 50 times faster than traditional Python lists.

The following line of code can be used to rename a python module while importing it.

import numpy as np

Updated on: 16-Sep-2022

402 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements