Modular programming is a way of dividing larger programs to smaller and atomic sub tasks for better management and to reduce complexity.
In Python, you must have heard of terms like modules, packages and libraries. However, it might be confusing for you to identify the true difference among them.
Let's demystify these concepts!
Python modules serve as an organizational unit of Python code. They are a bunch of related code grouped together.
You can have functions, classes or variables inside a Python module.
Let's say we have two files.
arithmetic.py file contains all the necessary mathematical operations you might use in your
Below is how
arithmetic.py program looks.
# variable author = "Pylenin" # functions def add(a, b): """Returns sum of 2 integers""" return a+b def subtract(a, b): """Returns difference of 2 integers""" return a-b def multiply(a, b): """Returns product of 2 integers""" return a*b def divide(a, b): """Returns division of 2 integers""" if b == 0: return "Invalid" return a/b # class class ArithmeticOperations: def __init__(self, a, b): self.a = a self.b = b def add(self): return self.a + self.b
It’s common to add similar codes within the same module and reuse it. So, you can just import the specific function you need.
You can import the variables, functions and the class into
main.py and perform the necessary operations. Such an arrangement keeps your code base clean and easy to debug.
from arithmetic import ArithmeticOperations from arithmetic import add from arithmetic import author print(author) print(add.__doc__) obj = ArithmeticOperations(2, 10) print(obj.add())
The above code returns the following output.
Pylenin Returns sum of 2 integers 12
Examples of modules in Python
- re (Regular Expression)
A package is a collection of various modules with a path attribute.
When you are building a large application, it might be difficult to maintain a lot of modules. That is where packages come into play.
The way we store files in different folders and sub folders based on a criteria, we can pack various python modules into one package.
To be considered a package, the directory must contain a file named
__init__.py. This file includes the initialization code for the corresponding package.
Why do you need init.py in a package?
As mentioned above,
__init.py__ contains the initialization code for the package. For example, you can control the functions you are exposing from the package by importing those specific functions into
Here is an example of a package called
my_awesome_package. We will import it to
main.py my_awesome_package | | ---__init__.py | | ---arithmetic.py | | ---welcome.py
arithmetic.py is same as the code mentioned above. The
welcome.py looks like the below.
def greet(): """Used for greetings""" return "Hello there"
Now, suppose you only want to expose the
add(a, b) and the
greet function from your awesome package. To achieve that, you have to import only those specific functions in your
from .arithmetic import add from .welcome import greet
__init__.py file allows you to control what you want to expose from your package.
Now when you import
main.py, you won't be able to access the other arithmetic functions apart from
add(a, b). You will get an
ImportError if you try to import any other function.
from my_awesome_package import add, greet, multiply print(add.__doc__) print(greet())
Traceback (most recent call last): File "main.py", line 1, in <module> from my_awesome_package import add, greet, multiply ImportError: cannot import name 'multiply' from 'my_awesome_package' (/home/runner/weffds/my_awesome_package/__init__.py)
Examples of Python packages
A Python library is a collection of modules and packages.
An interesting thing to note is that both module and package have technical meanings in the Python ecosystem while library does not. Developers of big packages with a lot of different tools tend to call them libraries.
The terms library and package are sometimes used interchangeably. Packages like Pandas and Numpy are also referred to as libraries.
Developers build Python libraries to share reusable code with the community to perform similar set of operations.
Examples of Python libraries
- Matplotlib - Used for data visualization
- Requests - Used for making HTTP requests in Python
- TensorFlow - Open source library for high level computations