A package is a folder containing multiple related modules, along with a special __init__.py file that marks the folder as an importable package rather than just a plain directory. Packages let a large project's modules be organized hierarchically, similar to how folders and subfolders organize files on a computer.
#how it works
1
Create a folder with an __init__.py inside
Even if that file is completely empty, its presence is what matters.
2
Place module files inside the folder
One or more .py files, organized however makes sense.
3
Import with dotted notation
import package_name.module_name reaches a module inside the package.
4
from package_name import module_name also works
Bringing the module itself in directly.
5
__init__.py can hold its own code
Commonly used to control exactly what’s exposed when the whole package is imported.
#example
# shapes/__init__.py
# (empty file — just marks this folder as a package)
# shapes/circle.py
def area(radius):
return 3.14 * radius ** 2
# main.py
import shapes.circle
print(shapes.circle.area(5))
from shapes import circle
print(circle.area(5))
Output
78.5
78.5
1The empty __init__.py file is what makes the shapes folder importable as a package, rather than just an ordinary directory Python would ignore.
2shapes/circle.py is a regular module, sitting inside the package folder.
3import shapes.circle accesses the module through its full dotted path, and shapes.circle.area(5) reaches the function inside it.
4from shapes import circle brings the module itself in directly, so it can be used as just circle.area(5) afterward.
#use cases
Organizing a large project's modules into a clear folder hierarchy, mirroring how the code is logically grouped
Structuring a library or framework that other people will install and import, like a typical PyPI package
Avoiding naming clashes between modules with similar names, by keeping them in separate packages
Controlling exactly what a package exposes to importers, using code inside its __init__.py
Practice Python interview questions
Every Python lesson maps to the matching interview topic, with answers written for current Python versions.
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses →Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free →Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books →