Skip to main content

Command Palette

Search for a command to run...

Day 3 - Python Modules and pip

Updated
9 min readView as Markdown
Day 3 - Python Modules and pip
S

🚀 Passionate DevOps Engineer with expertise in cloud computing, CI/CD, and automation. Skilled in Linux, Docker, Kubernetes, Terraform, Ansible, and Jenkins. I specialize in building scalable, secure, and automated infrastructures, optimizing software delivery pipelines, and integrating DevSecOps practices. Always exploring new ways to enhance deployment workflows and bridge the gap between development and operations.

Welcome to Day 3 of 100 Days of Python!

On Day 1, we learned what Python is and where it is used.

On Day 2, we explored the different kinds of applications that can be built with Python.

Today, we are going to learn two concepts that we will use throughout our Python journey:

  • Modules

  • pip

As we start building larger Python programs, we don't want to write every piece of functionality from scratch.

Python allows us to reuse existing code through modules and packages.


What is a Module?

A module is a Python file containing code that can be reused in another Python program.

A module can contain:

  • Variables

  • Functions

  • Classes

  • Constants

  • Other Python code

Instead of writing the same functionality repeatedly, we can put reusable code into a module and import it whenever we need it.

For example, imagine we have a file called:

calculator.py

It could contain:

def add(a, b):
    return a + b

We could then import it into another Python program:

import calculator

result = calculator.add(10, 20)
print(result)

Output:

30

This is one of the fundamental ideas behind code reuse in Python.


Types of Modules

Python modules can broadly be divided into two categories:

  1. Standard Library Modules

  2. Third-Party Modules


1. Standard Library Modules

Python comes with a large collection of modules as part of its standard library.

These modules are available with a normal Python installation, so we generally do not need to install them separately using pip.

For example:

import hashlib

hashlib is part of Python's standard library and provides common hashing algorithms.

Other examples include:

import math
import random
import os
import sys
import datetime

These modules provide functionality that we can reuse in our programs.


2. Third-Party Modules

Third-party modules/packages are created and maintained outside the Python standard library.

We can install them when we need additional functionality.

For example:

  • Pandas → Data analysis

  • NumPy → Numerical computing

  • Requests → HTTP requests

  • Flask → Web applications

  • FastAPI → APIs

  • OpenCV → Computer vision

  • Scikit-learn → Machine Learning

These packages greatly expand what Python can do.


What is pip?

pip is the standard package installer for Python.

It allows us to install Python packages from the Python Package Index (PyPI) and other package sources.

For example, if we want to install Pandas, we can run:

pip install pandas

After installation, we can import it into our Python program:

import pandas

Installing a Package with pip

Let's install Pandas.

Open a terminal or command prompt and run:

pip install pandas

pip will download Pandas and its required dependencies and install them into the appropriate Python environment.

After installation, we can use it in our Python program.

For example:

import pandas

df = pandas.read_csv("words.csv")

print(df)

Here:

  • import pandas imports the Pandas package.

  • pandas.read_csv() reads a CSV file.

  • df stores the resulting data structure.

We will learn Pandas properly later in the Python journey.


import in Python

The import statement allows us to use code from another module or package.

For example:

import math

print(math.sqrt(25))

Output:

5.0

Here:

import math

imports the math module.

We can then access its functionality using:

math.sqrt()

Importing Specific Items

We can also import a specific function or object from a module.

For example:

from math import sqrt

print(sqrt(25))

Output:

5.0

Instead of writing:

math.sqrt(25)

we can directly write:

sqrt(25)

Importing with an Alias

Sometimes module names are long or we simply want a shorter name.

Python allows us to create an alias using the as keyword.

For example:

import pandas as pd

Now we can use:

pd.read_csv("words.csv")

instead of:

pandas.read_csv("words.csv")

You will see this frequently in real-world Python code.

For example:

import numpy as np
import pandas as pd

These are common conventions in the Python ecosystem.


Standard Library vs Third-Party Packages

It is important to understand the difference.

Type Example Installation
Standard Library math Usually included with Python
Standard Library hashlib Usually included with Python
Standard Library random Usually included with Python
Third-Party pandas Usually installed separately
Third-Party numpy Usually installed separately
Third-Party requests Usually installed separately
Third-Party flask Usually installed separately

The standard library comes with Python, while third-party packages are installed separately when required.


Package vs Module

These terms are often used together, but they are not exactly the same.

Module

A module is generally a single Python file containing reusable code.

Example:

calculator.py

Package

A package is a way of organizing multiple Python modules into a larger reusable structure.

For example:

my_package/
    module1.py
    module2.py
    module3.py

Packages allow larger projects and libraries to organize their code into logical components.


Why Are Modules and Packages Important?

Imagine building a large application completely from scratch.

You would have to write everything yourself:

  • Mathematical functions

  • File handling

  • HTTP communication

  • Data processing

  • Database interaction

  • Machine Learning algorithms

  • Image processing

That would take an enormous amount of time.

Instead, Python developers reuse existing, tested functionality whenever appropriate.

For example:

Python
  │
  ├── Standard Library
  │
  ├── Third-Party Packages
  │       │
  │       ├── NumPy
  │       ├── Pandas
  │       ├── Requests
  │       ├── OpenCV
  │       └── Scikit-learn
  │
  └── Our Own Modules

This ecosystem is one of Python's biggest strengths.


Checking Installed Packages

We can use pip to see packages installed in an environment.

pip list

This displays installed Python packages and their versions.

We can also check information about a particular package:

pip show pandas

Installing a Specific Version

Sometimes a project requires a particular package version.

We can specify the version while installing:

pip install pandas==2.3.2

The exact version should depend on the requirements of the project.

We can also upgrade a package:

pip install --upgrade pandas

Removing a Package

If we no longer need a package, we can uninstall it:

pip uninstall pandas

pip will ask for confirmation before removing the package.


A Note About Virtual Environments

As Python projects become larger, installing every package globally can cause dependency conflicts.

For example:

Project A → requires Package X version 1
Project B → requires Package X version 2

A useful solution is to create a virtual environment for each project.

We will explore virtual environments and dependency management in more detail later.

For now, remember:

A virtual environment provides an isolated Python environment for a project and its dependencies.


Our Day 3 Code

Our basic demonstration contains both a third-party package and a standard-library module:

import pandas
import hashlib

print("Hi!")

Here:

import pandas

imports the third-party Pandas package.

And:

import hashlib

imports a module from Python's standard library.

The important point is that pandas normally needs to be installed separately, while hashlib is available as part of Python's standard library.


Important Commands

Here are the basic pip commands introduced today:

pip install package_name

Install a package.

pip uninstall package_name

Uninstall a package.

pip list

List installed packages.

pip show package_name

Show information about a package.

pip install --upgrade package_name

Upgrade a package.

pip install package_name==version

Install a specific package version.


Common Mistakes

Mistake 1: Forgetting to Install a Third-Party Package

If you write:

import pandas

without having Pandas installed in the active environment, Python may produce:

ModuleNotFoundError

Install it with:

pip install pandas

Mistake 2: Installing Packages in the Wrong Environment

You may install a package successfully but still receive:

ModuleNotFoundError

This can happen when pip installs the package into a different Python environment than the one running your program.

Virtual environments help prevent these problems.


Mistake 3: Confusing pip with import

Remember:

pip install pandas

is a terminal command used to install a package.

Whereas:

import pandas

is Python code used to import the package into your program.

They perform different jobs.


Quick Revision

Module

A reusable Python file containing code such as functions, classes, or variables.

Standard Library

Modules that are distributed with Python.

Examples:

math
random
os
sys
hashlib

Third-Party Package

Software developed outside Python's standard library and generally installed separately.

Examples:

pandas
numpy
requests
flask
opencv-python

pip

Python's standard package installer, commonly used to install and manage Python packages.

Import

Used to make a module or package available in our Python program.

import math

Alias

A different name given to an imported module.

import pandas as pd

Day 3 Takeaways

  1. Modules allow us to reuse Python code.

  2. Python provides a large standard library.

  3. Third-party packages extend Python's capabilities.

  4. pip is commonly used to install Python packages.

  5. import is used to access modules and packages in Python code.

  6. from ... import ... can import specific items.

  7. as can create an alias for an import.

  8. Virtual environments help isolate project dependencies.

  9. Python's package ecosystem is a major reason for its popularity.


Final Thought

One of the most powerful ideas in programming is:

Don't reinvent the wheel when reliable code already exists.

Instead of writing everything from scratch, Python allows us to build on top of a huge ecosystem of existing modules and packages.

Today we learned how to access that ecosystem.

Soon, we will start writing more of our own reusable code as well.

Day 3 complete.


📂 Day 3 Resources

👉 All notes and code for this day are available in the GitHub repository:

https://github.com/SriteshSuranjan/100-Days-of-Python/tree/main/03-Day03-Modules-and-Pip


100 Days of Python

Part 9 of 11

A structured 100-day journey to learn Python from the fundamentals to advanced concepts through consistent practice and hands-on coding. This series covers Python concepts step by step, including syntax, variables, data types, control flow, functions, data structures, object-oriented programming, exception handling, modules, file handling, libraries, and more. Each day includes clear notes, practical examples, and coding exercises to make learning easier and provide a useful reference for revision. The goal is to build a strong Python foundation that can be applied to automation, software development, data analysis, Artificial Intelligence (AI), Machine Learning (ML), and other areas of technology. Follow along, practice consistently, and build your Python skills one day at a time.

Up next

Day 2 - Applications of Python

Welcome to Day 2 of 100 Days of Python! On Day 1, we learned what programming is, what Python is, its major features, and where Python is used. Today, instead of learning a large amount of syntax, let