Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImportError: cannot import name 'Annotated' from 'pydantic.typing' when using inflect library in Python

Hello I am using Python 3.8

And I am trying to create a function to sort and print a list in the form of,

{first_element}, {second_element} and {last_element}

I've used the inflect library in Python to do this because it is expected to be used in my assignment.

import inflect
p = inflect.engine()

def sort():
    fruits = ["apple", "banana", "carrot"]
    mylist = p.join(fruits)
    print(mylist)

sort()

But when I execute this I get the following error:

Traceback (most recent call last):
  File "main.py", line 1, in <module>
    import inflect
  File "/home/username/.local/lib/python3.8/site-packages/inflect/__init__.py", line 77, in <module>
    from pydantic.typing import Annotated
ImportError: cannot import name 'Annotated' from 'pydantic.typing' (/home/username/.local/lib/python3.8/site-packages/pydantic/typing.py)

I tried reinstalling the library called Pydantic. But it didn't change anything.

like image 759
Shamal Lakshan Avatar asked Sep 13 '25 23:09

Shamal Lakshan


1 Answers

Pydantic 2.0 was released a few hours ago which has introduced breaking changes for inflect.

Ideally, inflect should have had an upper max for pydantic version in their project. Track the issue on inflect here.


Check what version of pydantic is installed on your machine. It must be 2.0. You need to uninstall that and install the highest 1.xx.x.


I understand this answer will be obsolete once this issue resolves, but here are the only two things to do when such breaking changes are encountered:

  1. Check the version you have installed in your machine.
  2. Check for issues on the project's GitHub page.
like image 62
Harshal Parekh Avatar answered Sep 15 '25 13:09

Harshal Parekh