What's the difference between a module and package in Python?
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Introduction
A Python module is a single .py file containing definitions and statements. A Python package is a directory containing multiple modules and an __init__.py file (or configured as a namespace package). Modules organize code within a file, packages organize modules into a directory hierarchy. You import a module by its filename and import a package by its directory name. Understanding the distinction is essential for structuring Python projects beyond simple scripts.
Modules
A module is any single .py file.
Every .py file you create is automatically a module. Python's standard library is also a collection of modules (os, sys, json, math, etc.).
Packages
A package is a directory containing an __init__.py file and one or more modules.
The __init__.py File
__init__.py runs when the package is imported. It can be empty or can define the package's public API.
Namespace Packages (Python 3.3+)
Python 3.3+ supports packages without __init__.py — these are namespace packages. They allow a package to span multiple directories.
Regular packages (with __init__.py) are preferred for most projects. Namespace packages are mainly used by large frameworks that split across multiple distributions.
Key Differences
| Feature | Module | Package |
| What it is | A single .py file | A directory of modules |
Requires __init__.py | No | Yes (regular) or No (namespace) |
| Contains | Functions, classes, variables | Modules and sub-packages |
| Import syntax | import module | import package.module |
__file__ attribute | Path to .py file | Path to __init__.py |
__path__ attribute | Not present | List of directory paths |
Relative Imports Within Packages
Common Pitfalls
- Missing
__init__.py: Without__init__.py, Python 2 does not recognize the directory as a package. Python 3 treats it as a namespace package, which behaves differently (no package-level code execution). Always include__init__.pyfor regular packages. - Circular imports: If
module_aimports frommodule_bandmodule_bimports frommodule_a, Python raisesImportErroror returns partially initialized modules. Break cycles by moving shared code to a third module or using lazy imports inside functions. - Shadowing standard library modules: Naming your file
json.pyoros.pyshadows the standard library module.import jsonwill import your file instead of the built-in. Never name your modules the same as standard library modules. - Running a module inside a package as a script: Running
python utils/math_utils.pydirectly makes Python treat it as a top-level script, breaking relative imports. Usepython -m utils.math_utilsto run it as a module within the package. - Confusing
import packagewith importing all modules:import utilsonly runsutils/__init__.py— it does not automatically import all modules inside the package. You must explicitly import each module (from utils import math_utils) or list them in__init__.py.
Summary
- A module is a single
.pyfile; a package is a directory containing modules and__init__.py - Packages organize related modules into a hierarchy (e.g.,
utils.math_utils,utils.io.file_utils) __init__.pydefines the package's public API and runs on import- Use relative imports (
.module) within packages for internal references - Never name your modules the same as standard library modules to avoid shadowing
Related reading
- What's the difference between a Python property and attribute?
- What's the difference between and vs list and dict?
- What's the difference between dist-packages and site-packages?
- What's the difference between Docker and Python virtualenv?
- What's the difference between Docker and Python virtualenv?
- What''s the difference between eval, exec, and compile?
- What's the difference between lists and tuples?
- What's the difference between lists and tuples?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.