Fix ComfyUI ImportError cannot import name __version__ from pydantic_core
In the ComfyUI community, a common issue has been cropping up lately where users encounter a specific failure during the initialization sequence. The error message usually looks like this: ImportError: cannot import name '__version__' from 'pydantic_core' (unknown location).
This error is particularly frustrating because it often happens after an update to a custom node or the ComfyUI core itself, even if you haven’t manually touched your pip packages.
Short Answer (for experienced devs)
Section titled “Short Answer (for experienced devs)”This error typically indicates a corrupted installation or a version mismatch between pydantic and pydantic-core. Since Pydantic v2, the core logic is moved to a Rust-based binary package (pydantic-core). If the metadata points to a version that isn’t fully installed or if there are “ghost” files from a previous version, the import fails.
The Immediate Fix: Force a clean re-installation of both packages within your specific environment.
# For standard Python environments (Python 3.10, 3.11, 3.12)python -m pip install --upgrade --force-reinstall pydantic pydantic-coreDeep Dive: Why is this happening?
Section titled “Deep Dive: Why is this happening?”The transition from Pydantic V1 to V2 was a massive architectural shift. Pydantic V2 relies on pydantic-core, a separate package containing compiled binaries. ComfyUI and its ecosystem of custom nodes often have conflicting dependency requirements.
If one node requires pydantic>=2.0 and another attempts to fallback to v1, or if a pip install was interrupted, you end up with a state where the Python wrapper for Pydantic expects certain attributes (like __version__) from the binary core that simply aren’t there or are incompatible.
Solution 1: The Clean Reinstall (Recommended)
Section titled “Solution 1: The Clean Reinstall (Recommended)”This is the most reliable fix. By using --force-reinstall, you tell pip to ignore the cached versions and current state, ensuring that the binary links are correctly established.
Note for Portable ComfyUI Users:
If you are using the “Portable” version of ComfyUI (the .7z or .zip release), you cannot just run pip globally. You must use the embedded Python executable.
# Illustrative example — run from your ComfyUI_windows_portable folder.\python_embeded\python.exe -m pip install --upgrade --force-reinstall pydantic pydantic-coreSolution 2: Manual Metadata Cleanup
Section titled “Solution 2: Manual Metadata Cleanup”Sometimes pip fails to overwrite existing .dist-info folders, leading to a “zombie” installation where Python thinks a package is present but cannot load it.
- Navigate to your environment’s
site-packagesdirectory. - Locate any folders starting with
pydanticorpydantic_core. - Delete them manually.
- Run a fresh install:
# Python 3.10+ illustrative examplepip install pydanticSolution 3: Handling Dependency Shadowing
Section titled “Solution 3: Handling Dependency Shadowing”In rare cases, another library might be bundling an older version of pydantic_core in a way that shadows the main installation.
Check for conflicting versions by running:
# Python 3.10+pip list | grep pydanticIf you see pydantic at version 1.x.x but pydantic-core is also present, you have a conflict. Pydantic V1 does not use pydantic-core. You should upgrade your environment to V2 unless a specific legacy node strictly requires V1.
Prevention Checklist
Section titled “Prevention Checklist”To avoid breaking your ComfyUI environment in the future, follow these practices:
- Isolate Environments: Never install ComfyUI dependencies into your global system Python. Use
venvorConda. - Check Custom Node Requirements: Before installing a new custom node, check its
requirements.txt. If it pinspydantic==1.10.x, it will likely break a modern ComfyUI setup. - Use the Manager: Use the ComfyUI-Manager custom node to install requirements. It handles many of these conflicts by running the correct pip flags under the hood.
Follow-up Questions
Section titled “Follow-up Questions”Does this error mean my GPU drivers are out of date? No. This is strictly a Python packaging issue. It relates to how libraries are loaded into memory and has no direct connection to your CUDA or GPU driver version.
What if I need Pydantic V1 for a specific non-ComfyUI project?
This is why Virtual Environments are critical. You can have pydantic==1.10 in one folder for an old project and pydantic==2.x in your ComfyUI folder without them ever seeing each other.
I reinstalled, but now I get “AttributeError: module pydantic has no attribute Field”?
This usually happens when you have a file named pydantic.py in your working directory. Python sees that file first and tries to import from it instead of the actual library. Ensure no local files share names with major libraries.