The Fix
pip install pydantic==2.7.2
Based on closed pydantic/pydantic issue #9221 · PR/commit linked
Production note: Most teams hit this during upgrades or environment changes. Roll out with a canary and smoke critical endpoints (health, OpenAPI/docs) before 100%.
@@ -220,145 +220,145 @@
# A mapping of {<member name>: (package, <module name>)} defining dynamic imports
_dynamic_imports: 'dict[str, tuple[str, str]]' = {
- 'dataclasses': (__package__, '__module__'),
+ 'dataclasses': (__spec__.parent, '__module__'),
# functional validators
Tested in 2.5.3 -> 2.7.0
pydantic version: 2.7.0
pydantic-core version: 2.18.1
pydantic-core build: profile=release pgo=true
install path: C:\Python311\Lib\site-packages\pydantic
python version: 3.11.7 (tags/v3.11.7:fa7a6f2, Dec 4 2023, 19:24:49) [MSC v.1937 64 bit (AMD64)]
platform: Windows-10-10.0.22631-SP0
related packages: typing_extensions-4.11.0
commit: unknown
Re-run the minimal reproduction on your broken version, then apply the fix and re-run.
Option A — Upgrade to fixed release\npip install pydantic==2.7.2\nWhen NOT to use: This fix is not applicable if the code is not run from a zip file.\n\n
Why This Fix Works in Production
- Trigger: Use of __package__ in pydantic/__init__.py fails if imported from zip
- Mechanism: __package__ is not set when importing from a zip file, causing import failures
- Why the fix works: Replaces the use of `__package__` with `__spec__.parent` in `pydantic/__init__.py` to ensure compatibility when importing from zip files. (first fixed release: 2.7.2).
- If left unfixed, the same config can fail only in production (env differences), causing startup failures or partial feature outages.
Why This Breaks in Prod
- Shows up under Python 3.9 in real deployments (not just unit tests).
- __package__ is not set when importing from a zip file, causing import failures
- Surfaces as: Traceback (most recent call last):\n File "<stdin>", line 1, in <module>\n File "<frozen importlib._bootstrap>", line 1055, in _handle_fromlist\n File…
Proof / Evidence
- GitHub issue: #9221
- Fix PR: https://github.com/pydantic/pydantic/pull/9331
- First fixed release: 2.7.2
- Reproduced locally: No (not executed)
- Last verified: 2026-02-09
- Confidence: 0.95
- Did this fix it?: Yes (upstream fix exists)
- Own content ratio: 0.54
Discussion
High-signal excerpts from the issue thread (symptoms, repros, edge-cases).
“Thanks @johanandersson-tietoevrycom for reporting this issue”
“I'm a bit lost, please can you format your issue using markdown so I can read the code.”
“Thanks @johanandersson-tietoevrycom for helping us to reproduce the problem. I created https://github.com/pydantic/pydantic/pull/9331 to fix the problem. It would be great if you can confirm the…”
“Hi, I am unsure of what you want me todo”
Failure Signature (Search String)
- Use of __package__ in pydantic/__init__.py fails if imported from zip
Error Message
Stack trace
Error Message
-------------
Traceback (most recent call last):\n File "<stdin>", line 1, in <module>\n File "<frozen importlib._bootstrap>", line 1055, in _handle_fromlist\n File "c:\tmp\testpydantic\pydantic.zip\pydantic\__init__.py", line 389, in __getattr__\n File "c:\Python39\lib\importlib\__init__.py", line 122, in import_module\n raise TypeError(msg.format(name))
Minimal Reproduction
Tested in 2.5.3 -> 2.7.0
pydantic version: 2.7.0
pydantic-core version: 2.18.1
pydantic-core build: profile=release pgo=true
install path: C:\Python311\Lib\site-packages\pydantic
python version: 3.11.7 (tags/v3.11.7:fa7a6f2, Dec 4 2023, 19:24:49) [MSC v.1937 64 bit (AMD64)]
platform: Windows-10-10.0.22631-SP0
related packages: typing_extensions-4.11.0
commit: unknown
Environment
- Python: 3.9
- Pydantic: 2
What Broke
Importing Pydantic from a zip file results in import errors and application crashes.
Why It Broke
__package__ is not set when importing from a zip file, causing import failures
Fix Options (Details)
Option A — Upgrade to fixed release Safe default (recommended)
pip install pydantic==2.7.2
Use when you can deploy the upstream fix. It is usually lower-risk than long-lived workarounds.
Fix reference: https://github.com/pydantic/pydantic/pull/9331
First fixed release: 2.7.2
Last verified: 2026-02-09. Validate in your environment.
When NOT to Use This Fix
- This fix is not applicable if the code is not run from a zip file.
Verify Fix
Re-run the minimal reproduction on your broken version, then apply the fix and re-run.
Did This Fix Work in Your Case?
Quick signal helps us prioritize which fixes to verify and improve.
Prevention
- Add a CI check that diffs key outputs after upgrades (OpenAPI schema snapshots, JSON payload shapes, CLI output).
- Upgrade behind a canary and run integration tests against the canary before 100% rollout.
Version Compatibility Table
| Version | Status |
|---|---|
| 2.7.2 | Fixed |
Related Issues
No related fixes found.
Sources
We don’t republish the full GitHub discussion text. Use the links above for context.