Register Login

Modules vs. libraries in Python

Updated Oct 11, 2021

It is sometimes confusing for newbies to understand the concept of modules and libraries of Python. From zoom-out content, you can get to know that both of them are set of codes bundled together. But there is a big difference between both of them. In this article, you will get to know about their differences and situations where they are used.

Modules and Libraries in Python:

Real-world programs are complex. Even a small software will have thousands of lines of code. That is why writing codes in continuous flow makes it difficult for programmers and developers to understand. To simplify the understanding and make it logically segmented, developers use modular programming. It is a technique of separating large coding tasks into shorter, logical, and more flexible subtasks.

One of the main focuses of Python is the ease of coding. It is the reason why Python has so many modules and libraries. Let us now understand each of these concepts and their types in details.

Modules:

Modules are a collection of related codes that are packed under a Python program. Programmers may decide whether to define functions, classes, or variables within a module. It’s also perfect to accommodate runnable codes within modules. In other words, they are Python files containing valid Python definitions and statements. These are normal file names that use the suffix .py when they are created. Clustering the related code into a module makes the code more straightforward to understand and implement. It also prepares the code organized in a logical manner. There are two different types of modules.

1. Predefined Modules: 

These are also called built-in modules. Python caters to a large collection of built-in modules. Programmers can use these modules in Python programs by directly importing them by invoking their name along with the keyword 'import'. E.g., import math. Most of the built-in Python modules are written in C and then combined with the Python interpreter to make the interpreter works along with these.

Some popular built-in Python modules are math, datetime, statistics, random, os, sys, etc.

Program:

from math import sqrt, factorial
print('Square root of 64: ',sqrt(64))
print('Factorial of 4: ',factorial(4))

Output:

Square root of 64:  8.0
Factorial of 4:  24

2. User-defined Modules:

Another superpower that Python renders to its programmers is the creation of user-defined modules. Python allows the programmers to customize their own operations and take control of their hands. Within a user-defined module, programmers can create their own set of functions, variables, and classes. The importing mechanism for the user-defined module is the same as that of predefined modules.

Program:

newModule.py:

def funcName(val):
print("You have passed the", val, "value as the parameter")

#funcName()

AnotherProgram:

import newModule
newModule.funcName(6)

Libraries:

A library is an umbrella term that comprises a reusable set of Python code/instructions. Usually, a Python library is a collection of related modules bundled together under one single name. It is popularly used by developers to share reusable code with the community. This eliminates the necessity for writing any Python code from scratch.

Developers and community researchers can create their own set of useful functions that are related to the same domain. Standard libraries come bundled with the Python interpreter when programmers and developers install it in their system. Some common examples of Python libraries are: matplotlib, Pygame, Pytorch, Requests, Beautifulsoap, etc.

Program:

import matplotlib.pyplot as mpl
a = [6, 4, 9]
b = [3, 5, 8]
mpl.plot(a, b)
mpl.xlabel('x_axis')
mpl.ylabel('y_axis')
mpl.title('Data Visualization')
mpl.show()

Output:

Difference between Modules and Libraries:

Modules Libraries
A module is a collection of code or functions that uses the .py extension. A Python library is a set of related modules or packages bundled together.
It is used by the programmers as well as the developers. It is mostly used by the community members, developers, and researchers.
Use of modules makes reading the code easier. Libraries do not contribute in better readability.
Modules logically cluster the functionality that programmers can import to reuse their code or set of statements. Libraries make the collection of logically related code reusable for the programming language users, developers, and other researchers.
Whenever a programmer imports a module in a Python program, the interpreter scans for several locations to look for the module's definition or body. We have to install the libraries in our Python project before using its modules or packages. Usually, we use the pip install command.
When a module is not found by Python's import statement, it searches for each directory within the shell variable i.e., PYTHONPATH. The PYTHONPATH is Python's environment variable consisting of a list of directories. Whenever the Python interpreter do not found the Python library associated with the project, it shows an error message and the program ends abruptly.
Modules are mostly written using valid Python statements or codes. Libraries, mainly standard libraries, are mostly written using C language or Python.
The main focus of creating modules is to avoid DRY (Don’t Repeat Yourself). Libraries do not have any such objective.
We can use the Python’s built-in dir() function to return a sorted list of strings holding the function names defined within a module. There is not such explicit function that can return the number of modules a library contain. Still, programmers can use the help() to extract some information.
Example of popular built-in Python modules are os, sys, math, random, etc. Example of popular built-in Python libraries are Pygame, Pytorch, matplotlib, etc.

Conclusion:

If you are a programmer who is doing a normal project using Python, he/she should try to cluster the code into small chunks of functions and bring them under one module. If you are a developer or a researcher working for the Python community or working for any company that wants a large bundle of similar modules, bring the modules under one library.


×