Modules & Packages
Modules, packages, imports, and code organization.
5 questions
JuniorTheoryVery commonWhat is __name__, and when does it equal "__main__"?
What is __name__, and when does it equal "__main__"?
Every module has a __name__. When imported, __name__ holds the module's name. When the file is run directly as a script, Python sets __name__ to "__main__", enabling the if __name__ == "__main__": guard.
Common mistakes
- ✗Believing
__name__is always"__main__", so the guard runs on import too - ✗Thinking
__name__always equals the filename regardless of how it is run - ✗Putting startup code outside the
if __name__ == "__main__":guard, so it runs on import
Follow-up questions
- →What value does
__name__hold inside a module imported aspackage.item? - →Why is the
if __name__ == "__main__":guard considered a best practice?
JuniorTheoryCommonWhat is a module in Python?
What is a module in Python?
A module is a single .py file of Python code — a reusable unit you import. The first import runs the file once and binds its names under the module object. Modules group related code and bundle into packages.
Common mistakes
- ✗Thinking each
importre-runs the file — it executes only once, then is cached - ✗Believing a module must be a directory rather than a single
.pyfile - ✗Confusing a module (one
.pyfile) with a package (a directory of modules)
Follow-up questions
- →Where does Python cache already-imported modules so the file runs only once?
- →How does a
.pyfile differ from a package directory with an__init__.py?
JuniorTheoryCommonWhat is a package, and what makes a directory one?
What is a package, and what makes a directory one?
A package is a directory that groups modules and acts as a namespace. A regular package holds an __init__.py, which runs on import and may be empty. Every package is a module, but not every module is a package.
Common mistakes
- ✗Assuming any directory is a package without an
__init__.pymarker - ✗Thinking a package is not itself a module, so importing the package name alone does nothing
- ✗Thinking
__init__.pymust contain code, when it may be empty
Follow-up questions
- →What runs, and when, the first time you import
package.item? - →How does a regular package differ from a namespace package?
MiddleTheoryOccasionalHow does Python locate a module when you import it?
How does Python locate a module when you import it?
Python scans sys.path in order: the script's directory (or cwd), then PYTHONPATH entries, then standard-library locations. sys.path is a mutable list, and the first directory that matches wins the import.
Common mistakes
- ✗Believing Python scans the whole filesystem instead of just
sys.path - ✗Thinking
sys.pathis immutable, when it is a mutable list you can edit - ✗Forgetting the first match in
sys.pathwins, so order shadows later entries
Follow-up questions
- →How can you add a directory to
sys.pathat runtime, and when is that risky? - →What is module shadowing, and how does
sys.pathorder cause it?
SeniorTheoryRareWhat is a namespace package, and how does it differ from a regular one?
What is a namespace package, and how does it differ from a regular one?
A namespace package (PEP 420, Python 3.3+) is a package with NO __init__.py. It may be split across MULTIPLE directories on sys.path, which Python merges into one logical package. A regular package has __init__.py in one directory.
Common mistakes
- ✗Believing every package always needs an
__init__.py, even namespace ones - ✗Thinking a namespace package cannot span multiple directories on
sys.path - ✗Confusing a regular package (one dir, has
__init__.py) with a namespace one
Follow-up questions
- →How does Python merge two directories with the same package name on
sys.path? - →When would splitting one package across separately installed distributions help?