# Day 3 - Python Modules and pip

![](https://cdn.hashnode.com/uploads/covers/664f77938fc1f806b829b90b/1130fb59-06a6-423d-ac69-77d4e650d5eb.jpg align="center")

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:

```text
calculator.py
```

It could contain:

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

We could then import it into another Python program:

```python
import calculator

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

Output:

```text
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:

```python
import hashlib
```

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

Other examples include:

```python
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:

```bash
pip install pandas
```

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

```python
import pandas
```

* * *

# Installing a Package with pip

Let's install Pandas.

Open a terminal or command prompt and run:

```bash
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:

```python
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:

```python
import math

print(math.sqrt(25))
```

Output:

```text
5.0
```

Here:

```python
import math
```

imports the `math` module.

We can then access its functionality using:

```python
math.sqrt()
```

* * *

# Importing Specific Items

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

For example:

```python
from math import sqrt

print(sqrt(25))
```

Output:

```text
5.0
```

Instead of writing:

```python
math.sqrt(25)
```

we can directly write:

```python
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:

```python
import pandas as pd
```

Now we can use:

```python
pd.read_csv("words.csv")
```

instead of:

```python
pandas.read_csv("words.csv")
```

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

For example:

```python
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:

```text
calculator.py
```

### Package

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

For example:

```text
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:

```text
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.

```bash
pip list
```

This displays installed Python packages and their versions.

We can also check information about a particular package:

```bash
pip show pandas
```

* * *

# Installing a Specific Version

Sometimes a project requires a particular package version.

We can specify the version while installing:

```bash
pip install pandas==2.3.2
```

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

We can also upgrade a package:

```bash
pip install --upgrade pandas
```

* * *

# Removing a Package

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

```bash
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:

```text
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:

```python
import pandas
import hashlib

print("Hi!")
```

Here:

```python
import pandas
```

imports the third-party Pandas package.

And:

```python
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:

```bash
pip install package_name
```

Install a package.

```bash
pip uninstall package_name
```

Uninstall a package.

```bash
pip list
```

List installed packages.

```bash
pip show package_name
```

Show information about a package.

```bash
pip install --upgrade package_name
```

Upgrade a package.

```bash
pip install package_name==version
```

Install a specific package version.

* * *

# Common Mistakes

## Mistake 1: Forgetting to Install a Third-Party Package

If you write:

```python
import pandas
```

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

```text
ModuleNotFoundError
```

Install it with:

```bash
pip install pandas
```

* * *

## Mistake 2: Installing Packages in the Wrong Environment

You may install a package successfully but still receive:

```text
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:

```bash
pip install pandas
```

is a **terminal command** used to install a package.

Whereas:

```python
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:

```python
math
random
os
sys
hashlib
```

### Third-Party Package

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

Examples:

```text
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.

```python
import math
```

### Alias

A different name given to an imported module.

```python
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] 

* * *
