100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Python

Modules and Packages in Python

Learn how Python organizes code into reusable modules and packages, and how the import system locates and caches them.

Modules, Files & IterablesBeginner9 min readJul 7, 2026
Analogies

1. Introduction

A module is simply a single .py file containing Python code (functions, classes, variables) that can be reused in other programs. A package is a directory of related modules grouped together, traditionally identified by an __init__.py file. Modules and packages let you split large programs into organized, reusable, and maintainable pieces instead of writing everything in one file.

🏏

Cricket analogy: A single training drill file like 'batting_drills.py' is a module, while a whole 'fielding' folder holding drills for slips, gully, and boundary practice together is a package -- splitting a huge training manual into organized pieces.

Python ships with a large standard library of built-in modules (such as math, os, and json), and you can also install third-party packages using pip, or write your own modules and import them into any script.

🏏

Cricket analogy: Python's standard library modules like math and os are like the umpire's official rulebook always on hand, pip packages are like hiring a specialist statistician (pandas) for advanced analysis, and your own module is your personal scouting notebook.

2. Syntax

python
# Import an entire module
import math

# Import a specific name from a module
from math import sqrt

# Import with an alias
import math as m
from math import sqrt as square_root

# Import multiple names
from math import pi, sqrt

# Package structure (folder layout)
# mypackage/
#     __init__.py
#     module_a.py
#     module_b.py
from mypackage import module_a

3. Explanation

When you write 'import module_name', Python searches sys.path (the current directory, installed site-packages, and standard library locations) for a matching module. Once found, the module's code runs top to bottom exactly once, and the resulting module object is cached in sys.modules.

🏏

Cricket analogy: 'import fielding_drills' is like a coach checking the training folder, kit bag, and equipment shed (sys.path) for the right drill sheet; once found, it's read start to finish once and filed in the coach's reference binder (sys.modules) for reuse.

A package is a directory that Python treats as a namespace of modules. Traditionally, a package needs an __init__.py file (even an empty one) to mark the directory as a package, though Python 3.3+ also supports 'implicit namespace packages' that work without an __init__.py in some cases. Using an __init__.py is still the clearest, most compatible way to define a package.

🏏

Cricket analogy: A 'fielding' folder becomes an official package once it has an __init__.py marker, like a training folder becoming an officially sanctioned drill set once the academy stamps it -- newer setups can skip the stamp, but stamping is still clearest.

Import caching: no matter how many times a module is imported across a program, its top-level code only executes once. Subsequent imports just reuse the cached object from sys.modules, which is why side effects (like print statements) at module level only appear the first time.

4. Example

python
import math
from math import sqrt as square_root

print(math.pi)
print(square_root(16))

import sys
print('mymodule' in sys.modules)

import math  # already cached, does not re-run
print('math' in sys.modules)

5. Output

text
3.141592653589793
4.0
False
True

6. Key Takeaways

  • A module is a single .py file; a package is a directory of modules.
  • Packages traditionally require an __init__.py, though implicit namespace packages exist in Python 3.3+.
  • Modules are executed only once per program; later imports reuse the cached sys.modules entry.
  • You can import a whole module, specific names, or use aliases with 'as'.
  • Python locates modules by searching sys.path in order.

Practice what you learned

Was this page helpful?

Topics covered

#Python#PythonProgrammingStudyNotes#Programming#ModulesAndPackagesInPython#Modules#Packages#Syntax#Explanation#StudyNotes#SkillVeris