Why a File Named json.py Can Break the Standard Library
What a Python.org post says module shadowing is, how a file in your project can take precedence over the standard library and what causes it.
For: Learners who write Python scripts and have seen an import error.
What the problem looks like
The issue manifests as seemingly random AttributeError exceptions from modules like json, math, or http for names you know are correct.[1]
What shadowing is and why a project file wins
The issue is usually that a module is “shadowing” the standard library module from a higher-precedence location in sys.path, such as your current working directory or a directory on PYTHONPATH.[1]
There is probably a file named json.py or math.py in your project, and that module takes precedence over the standard library module of the same name, which is only noticed once you start using the module elsewhere in your project.[1]
What causes it
This issue is caused by Python’s flat module namespace; there is no delineation between what is part of the standard library and what are third-party modules, either dependencies or project code.[1]
Sources
- One namespace to namespace them all (blog.python.org · checked )
More articles
- LearningWhy Rust for CPython, and What It Will Not FixThe reasons the Language Summit 2026 write-up gives for proposing Rust in CPython, what it says Rust does not remove and how the team proposes to reduce the risk.
- LearningWhy One Table Needs Several Models, and the Idea PEP 827 OffersWhat the Language Summit write-up says each CRUD operation needs from a model, and the idea PEP 827 offers for keeping them in step.
- LearningWhat the GIL, Subinterpreters and Free-Threading Each Give UpWhat the Python Language Summit 2026 write-up says each of Python's three concurrency models gives up, and why it calls threads a hard interface.