March 17, 2026 · All About CS
Python Packages & PIP Explained: Install, Use, and Manage External Libraries
A complete beginner's guide to Python packages and PIP — learn what packages are, how PyPI works, and how to install, use, and uninstall external libraries like NumPy from the command line.
Python Packages & PIP Explained: Install, Use, and Manage External Libraries
Python's real superpower isn't just the language itself — it's the enormous ecosystem of packages you can pull into any project in seconds. This guide demystifies how that ecosystem works: what packages are, where they live, and how to install, use, and remove them using PIP.
Key Takeaways
- A package is a folder-based way of organizing related Python modules into a directory hierarchy.
- Python ships with a large standard library of built-in modules that need no installation.
- External libraries (NumPy, Pandas, Flask, etc.) live on the Python Package Index (PyPI) and must be installed before use.
- PIP is the command-line tool that comes bundled with Python for installing and managing packages from PyPI.
- Installing a package is as simple as
pip install <package-name>.
Quick Recap: Modules vs. Libraries vs. Packages
Before diving into PIP, it helps to have these three terms clear in your head — they're often used interchangeably, but they refer to slightly different things:
| Term | What it is |
|---|---|
| Module | A single .py file containing reusable functions, classes, and variables |
| Library | A collection of modules designed to tackle a specific set of tasks |
| Package | A directory that organizes related modules (and sub-packages) into a hierarchy |
In practice, "library" and "package" are used interchangeably in the Python community. When someone says "install the NumPy package," they mean the same thing as "install the NumPy library." What distinguishes a package structurally is that it's a folder with an __init__.py file, which can contain many modules, sub-packages, documentation, and data files.
numpy/ ← the package (a directory)
├── __init__.py ← marks it as a Python package
├── core/ ← a sub-package
│ ├── __init__.py
│ └── multiarray.py ← a module
├── random/ ← another sub-package
│ └── ...
└── ...Standard Library vs. External Libraries
Python ships with a standard library — a large collection of modules that are immediately available after installing Python, no extra steps required. Some common ones:
import math # mathematical functions
import json # JSON encoding and decoding
import os # operating system interface
import http # HTTP protocol support
import platform # system/platform information
import array # efficient numeric arraysExternal libraries, on the other hand, are not bundled with Python. They're developed and maintained by the community, and you need to install them before you can import them. Popular examples include:
- NumPy — high-performance numerical arrays and math
- Pandas — data manipulation and analysis
- Flask — lightweight web framework
- TensorFlow — machine learning and deep learning
- Pygame — 2D game development
- Tkinter — desktop GUI applications (technically bundled, but platform-dependent)
Where do external packages live? The Python Package Index (PyPI) is the official repository for external Python packages. At the time of writing, PyPI hosts over 500,000 packages. When you run pip install numpy, PIP fetches the package from PyPI and installs it on your machine.
Part 1 — PIP: Python's Package Manager
PIP stands for Pip Installs Packages (a recursive acronym). It comes bundled with Python 3.4+ and is the standard tool for installing packages from PyPI.
Verify PIP is Installed
Before installing anything, confirm PIP is available on your system:
pip --version
# Example output: pip 24.0 from /usr/lib/python3/dist-packages/pip (python 3.11)If the command isn't found, you can bootstrap it with:
python -m ensurepip --default-pipLinux users: Depending on your system, you may need to use pip3 instead of pip to ensure you're targeting Python 3. Run pip3 --version to check. This mirrors the python3 vs python distinction covered in the environment setup guide.
Part 2 — Installing a Package
Let's use NumPy as a concrete example. If you try to import it before installing, Python throws a clear error:
import numpy as np
# ModuleNotFoundError: No module named 'numpy'That error tells you exactly what to do: install it. One command is all it takes:
pip install numpyPIP resolves dependencies, downloads the package from PyPI, and installs it. The terminal output ends with something like:
Successfully installed numpy-1.26.4Now the import works without errors:
import numpy as np
print(np.__version__) # e.g. 1.26.4Installing a Specific Version
If a project requires a particular version of a library, you can pin it:
pip install numpy==1.24.0 # exact version
pip install numpy>=1.23,<2.0 # version rangePart 3 — Using NumPy: Practical Examples
NumPy is best known for fast, flexible N-dimensional arrays and vectorized math operations. Here are a few hands-on examples to get a feel for it.
Generating Random Integers
import numpy as np
# Generate a single random integer between 0 and 9 (inclusive)
random_number = np.random.randint(0, 10)
print(random_number) # e.g. 7np.random.randint(low, high) returns a random integer in the range [low, high) — the upper bound is exclusive.
Creating a 2D Array
import numpy as np
# Create a 2D array (matrix) from a list of lists
arr = np.array([[1, 2, 3],
[4, 5, 6]])
print(arr)
# [[1 2 3]
# [4 5 6]]
print(arr.ndim) # 2 — number of dimensions
print(arr.shape) # (2, 3) — 2 rows, 3 columns
print(arr.dtype) # int64 — data type of elements🖼️ Visual Suggestion: Diagram showing a 2D NumPy array laid out as a grid with row/column indices labeled.
Basic Array Operations
One of NumPy's key advantages over plain Python lists is element-wise operations — no loops needed:
import numpy as np
a = np.array([1, 2, 3, 4, 5])
print(a * 2) # [2 4 6 8 10] — multiply every element by 2
print(a ** 2) # [ 1 4 9 16 25] — square every element
print(a.mean()) # 3.0
print(a.sum()) # 15
print(a.max()) # 5NumPy is just the starting point. Libraries like Pandas and Matplotlib are built on top of NumPy arrays. Learning NumPy's array model pays dividends across the entire Python data science ecosystem.
Part 4 — Managing Installed Packages
List All Installed Packages
pip list
# Output: a table of all installed packages and their versionsShow Details About a Specific Package
pip show numpy
# Name: numpy
# Version: 1.26.4
# Summary: Fundamental package for array computing in Python
# ...Uninstalling a Package
When you no longer need a package:
pip uninstall numpyPIP will ask for confirmation, then remove the package. Attempting to import it afterward restores the original error:
import numpy as np
# ModuleNotFoundError: No module named 'numpy'Saving and Recreating Your Environment
On real projects, you'll want to record exactly which packages (and versions) the project depends on. The convention is a requirements.txt file:
# Save all currently installed packages to a file
pip freeze > requirements.txt
# Later, recreate the same environment on another machine
pip install -r requirements.txt🖼️ Visual Suggestion: Diagram showing the PyPI → PIP → local environment flow, with
requirements.txtconnecting two machines.
PIP Command Cheat Sheet
pip --version # check PIP version
pip install numpy # install latest version
pip install numpy==1.24.0 # install specific version
pip install -r requirements.txt # install from requirements file
pip uninstall numpy # remove a package
pip list # list all installed packages
pip show numpy # details about a package
pip freeze > requirements.txt # export installed packages
pip search numpy # search PyPI (if enabled)What's Next?
With PIP in your toolkit, the entire Python ecosystem is at your fingertips. In the next part of this series, we'll start working with one of those packages hands-on — diving into object-oriented programming in Python, where you'll learn how to model real-world problems using classes and objects.
Happy coding. 🐍