Python Packages: Grouping Modules with __init__.py

A package groups Python modules under an import name. A regular package has an __init__.py file, which runs when it is first imported. Namespace packages can omit this file. Use packages to organize related code.
Why it exists
When a program grows beyond a single file, you need a way to organize related code. Python's modules (.py files) are the first step, but packages provide the next level of hierarchy, allowing you to group related modules into a single logical namespace.
The mental model
A package groups modules under an import namespace. A regular package is usually a directory containing an init.py file. The regular-package examples below use this structure. Namespace packages are a different kind that can span multiple locations without an init.py file.
How it works
When you write import my_package, Python searches for a directory named my_package that contains an init.py file. Upon finding it, Python executes the contents of init.py to initialize the package. This file can be empty, which is the simplest case. Or, it can contain Python code to set up package-level variables, automatically import key functions from its sub-modules to make them easier to access, or define a special list called all to explicitly name what should be imported when a user runs from my_package import *.
When to use it
Use packages for any project with more than a few files. It's the standard way to structure libraries and applications. For example, a web application might have packages for api, database, and services to keep the code clean and maintainable.
When not to use it
For very small, single-purpose scripts, a single .py module is perfectly fine. Introducing a package structure can be overkill and add unnecessary complexity if your entire program fits comfortably in one file.
One canonical example
Imagine a directory utils with two files: init.py and formatters.py. Inside formatters.py, you have a function format_date(). If the init.py file is empty, you must import the function using from utils.formatters import format_date. However, if you add from .formatters import format_date inside your init.py file, you create a shortcut. Now, users of your package can simply write from utils import format_date, making for a cleaner public API.
Interview question
What is the primary function of an __init__.py file in a Python directory?
- a.To automatically import all sub-modules into the package's namespace.
- b.To declare the package's version number and author information.
- c.To explicitly designate the directory as a Python package.Correct
- d.To define the package's main entry point for script execution.
Why? this is the answer
For a regular package, __init__.py historically marks the package and can run initialization code, so C is the intended answer in this question. Namespace packages can omit the file; importing submodules or exposing a public API is optional behavior, not automatic.
Just read this? Test yourself on what you have been reading.
Read the original → docs.python.org
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on python — each one lists the topics its interview covers.
See open roles