Why is import bad?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
from module import * (wildcard import) imports every public name from a module into your current namespace. It is considered bad practice because it pollutes the namespace, hides where names come from, causes silent name collisions, and makes code unpredictable for both humans and tools. PEP 8 explicitly discourages it for production code.
What import * Does
"Public names" means everything in the module's __all__ list, or if __all__ is not defined, every name that does not start with an underscore.
Problem 1: Namespace Pollution
With explicit imports, the collision is visible:
Problem 2: Readability — Where Did This Come From?
A reader (or your future self) cannot determine which module provides array or mean without checking every wildcard import. With explicit imports:
Problem 3: Silent Name Shadowing
Problem 4: Breaks Static Analysis
IDEs lose the ability to provide autocompletion, go-to-definition, and refactoring support.
Problem 5: Fragile Dependencies
A library update can break your code without changing any of your own lines.
When import * Is Acceptable
Even in __init__.py, many projects prefer explicit re-exports for clarity.
The Right Way to Import
Controlling __all__
If you maintain a module, define __all__ to limit what import * exports:
PEP 8 Guidance
PEP 8 states:
Wildcard imports (from module import *) should be avoided, as they make it unclear which names are present in the namespace, confusing both readers and many automated tools.The only exception PEP 8 acknowledges is re-publishing an internal API as part of a public package interface.
Common Pitfalls
- Built-in shadowing: Modules like
osexportopen, andbuiltinsnames likefilter,map,listcan be shadowed by wildcard imports from libraries that define functions with the same names. - Circular imports:
import *in circular dependency chains causes partially-initialized modules andImportErrororAttributeErrorat runtime. - Testing: Wildcard imports make mocking and patching harder because
from module import *copies references — patching the original module does not affect the already-imported names. __all__is optional: If a module does not define__all__,import *imports everything without underscores. This can be hundreds of names from large modules.- Linter configuration: Enable
F403(wildcard import) in flake8 orwildcard-importin pylint to catch these automatically.
Summary
from module import *imports all public names, polluting the namespace- It hides where names come from, breaks IDE tooling, and causes silent name collisions
- Library updates can introduce new names that shadow builtins or your own variables
- Use
import moduleorfrom module import specific_nameinstead - PEP 8 discourages wildcard imports in production code
- Define
__all__in your own modules to control what gets exported

