Modules, packages and libraries in Python

Learn the difference between modules, packages and libraries and ways to work with them in Python 3.

Modules, packages and libraries in Python

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

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.


The file contains all the necessary mathematical operations you might use in your program.

Below is how 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 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



obj = ArithmeticOperations(2, 10)

The above code returns the following output.


Returns sum of 2 integers

Examples of modules in Python

  1. datetime
  2. csv
  3. 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 This file includes the initialization code for the corresponding package.

Why do you need 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 file.

Here is an example of a package called my_awesome_package. We will import it to


The is same as the code mentioned above. The 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 file.

from .arithmetic import add
from .welcome import greet

Having this file allows you to control what you want to expose from your package.

Now when you import my_awesome_package in, 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



Traceback (most recent call last):
  File "", 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/

Examples of Python packages

  1. Numpy
  2. Pandas
  3. Pytest

Python Libraries

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

  1. Matplotlib - Used for data visualization
  2. Requests - Used for making HTTP requests in Python
  3. TensorFlow - Open source library for high level computations

Subscribe to Pylenin

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.