What is the difference between a module and a package in Python?

A module is a single Python file (.py) that has functions, classes or variables. A package is a folder that groups related modules together.

import math
print(math.sqrt(16))

from math import pi
print(pi)

Output:

Tutorials Class - Output Window

4.0
3.141592653589793

A package looks like this on disk:

shop/
    __init__.py
    cart.py
    payment.py
from shop import cart
from shop.payment import pay

The __init__.py file marks the folder as a regular package. Python can also use folders without it, but most projects still keep the file. Python comes with many built-in modules, and you can install more with pip.


Learn more about the similar topics:
Tutorials
Python Decorators
Python Closures
Python Iterators and Generators
Python Modules
Exercises & Assignments
No Content Found.
Interview Questions & Answers
What is a decorator in Python?
What are iterators in Python?
What is a generator in Python?
What is the GIL in Python?
What is the difference between a module and a package in Python?
How does Python manage memory?
What is the with statement in Python?