Libraries, Packages, and Modules in Python#
đ Data Analysis Using Python đź NumPy & pandas Lesson 026
â Previous ¡ Next ✠¡ â Section ¡ â Hub
Important
⨠AI-generated content. This page was written with the assistance of an AI language model and is provided as a learning aid. Despite careful review, it may still contain mistakes, omissions, or out-of-date information. Whether you are new to the topic, a team lead, or a senior practitioner, treat it as a starting point rather than an authoritative reference: read it critically and independently verify anything you act on (code, commands, figures, and factual claims) against official documentation and primary sources before relying on it.
Standing on othersâ code#
Pythonâs true power for data analysis comes not from the language alone but from its vast ecosystem of libraries â reusable code others have written, imported and used rather than reinvented. Opening the final stage, this lesson covers libraries, packages, and modules: what they are, how to import them, and why they make Python the dominant data tool.
Modules, packages, libraries#
The terms nest:
A module is a single file of Python code (functions, classes, values) that can be imported and used elsewhere.
A package is a collection of modules organised together (a directory of related modules).
A library is, loosely, a body of reusable code â a package or set of packages â providing functionality for some purpose (numerical computing, data analysis, visualization).
The distinctions are technical; in practice âlibraryâ is used broadly for importable code you use rather than write. The point is reuse at scale â leveraging code the community has built, tested, and maintained.
Importing#
Code from a library is brought in with import:
import math # import a module
math.sqrt(16) # use it with the module name: 4.0
import numpy as np # import with an alias (convention)
np.array([1, 2, 3]) # use via the alias
from statistics import mean # import a specific name
mean([1, 2, 3]) # use it directly: 2
import numpy as np brings in the NumPy library under the short alias np (a universal
convention); from ... import ... brings a specific name into direct use. These import
forms are how every library in the coming lessons is accessed â import numpy as np,
import pandas as pd are the standard incantations.
The data ecosystem#
Pythonâs data-analysis dominance rests on its libraries, the key ones being:
NumPy â fast numerical computing on arrays (the next lessons).
pandas â tabular data analysis, the DataFrame (the lessons after).
matplotlib / others â visualization.
and a vast constellation for statistics, machine learning, and more.
These libraries do the heavy lifting â decades of optimised, tested code â so an analyst composes existing powerful tools rather than building from scratch. This is the reuse principle at the ecosystem scale: the reason Python is the data language is largely this library ecosystem.
The caveat#
Libraries are indispensable but bring their own considerations. Dependencies â the libraries your code relies on â must be installed and, importantly, versioned: code written for one version of a library may behave differently or break on another, so managing which versions are used matters for reproducibility (the reproducibility theme, at the dependency level). Importing many libraries also has a cost, and reaching for a heavy library for a task the standard tools handle is its own over-engineering. The disciplines: use well-established, maintained libraries; be aware of version compatibility; and import what the task genuinely needs. The ecosystem is Pythonâs great strength, used deliberately. The next lessons dive into the first essential library: NumPy.
Hint
See also
Source article Adapted (context, re-expressed) in our own words from: https://insightful-data-lab.com/2023/12/06/libraries-packages-and-modules-in-python/ (insightful-data-lab.com).