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
# 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_a3. 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
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
3.141592653589793
4.0
False
True6. 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
1. What file traditionally marks a directory as a Python package?
2. How many times does a module's top-level code execute if it is imported from three different files in the same program?
3. Which statement imports only the sqrt function from the math module under the name square_root?
4. Where does Python look when resolving an 'import module_name' statement?
5. What Python feature introduced in 3.3 allows a package-like directory to work without an __init__.py?
Was this page helpful?
You May Also Like
File Handling in Python
Understand how to open, read, write, and close files in Python using the built-in open() function and file modes.
os Module in Python
Explore Python's os module for interacting with the operating system, including file paths, directories, and environment information.
Virtual Environments and pip in Python
Isolating project dependencies with venv and managing packages with pip -- creation, activation, and requirements files.
with Statement (Context Managers) in Python
Learn how the 'with' statement uses the context manager protocol (__enter__/__exit__) to guarantee cleanup, such as automatically closing files.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics