Search code examples
pythonpydanticprefect

Prefect : ImportError: cannot import name 'SecretField' from 'pydantic'


I'm currently using prefect to orchestrate some simple task in python. It's working fine until I get this error :

Traceback (most recent call last):
  File "test.py", line 2, in <module>
    from prefect import flow
  File "/Users/.../.venv/lib/python3.8/site-packages/prefect/__init__.py", line 25, in <module>
    from prefect.states import State
...

ImportError: cannot import name 'SecretField' from 'pydantic' (/Users/.../.venv/lib/python3.8/site-packages/pydantic/__init__.py)

It's seems I have the module install in my venv :

(.venv) User@user % pip show pydantic
Name: pydantic
Version: 2.3.0
Summary: Data validation using Python type hints
Home-page: None
Author: None
Author-email: Samuel Colvin <[email protected]>, Eric Jolibois <[email protected]>, Hasan Ramezani <[email protected]>, Adrian Garcia Badaracco <[email protected]>, Terrence Dorsey <[email protected]>, David Montague <[email protected]>
License: None
Location: /Users.../.venv/lib/python3.8/site-packages
Requires: annotated-types, pydantic-core, typing-extensions
Required-by: prefect, fastapi

Where this could come from ?


Solution

  • That's because Prefect isn't compatible with pydantic>2 yet.
    So, they've pinned the reqs to the versions less than 2 (check PR10144 for more details).

    Try to install the latest version of Perfect or downgrade your pydantic's to 1.10.11 :

    pip install -U prefect 
    # or pip install pydantic==1.10.11