Home > database >  Python Django ImportError: cannot import name 'Required' from 'typing_extensions'
Python Django ImportError: cannot import name 'Required' from 'typing_extensions'

Time:11-06

Django version is 3.2.9.

Python version is 3.10.0.

And typing_extensions 3.10.0.2

I'm new to coding, python, etc., and can't figure out what is the problem. Following django tutorial I created an app and ran the server successfully, but a day later, when I tried to do that again I faced this problem:

File "C:\Users\fused\Desktop\code\py\myproject\myapp\views.py", line 1, in <module>
from typing_extensions import Required
ImportError: cannot import name 'Required' from 'typing_extensions' (C:\Users\fused\AppData\Local\Programs\Python\Python310\lib\site-packages\typing_extensions.py)

After trying to run a server with 'python manage.py runserver' this problem appeared, tried reinstalling typing_extensions, checked versions of everything, but nothing solved the problem.

If any additional information is needed, I'll reply with it. Thanks in advance

CodePudding user response:

It seems like Required and NotRequired aren't implemented yet in typing_extensions.

PEP 655 states:

The goal is to be able to make the following statement:

The mypy type checker supports Required and NotRequired. A reference implementation of the runtime component is provided in the typing_extensions module.

It's just the goal — it isn't the current state. It is neither listed in typing_extensions' README nor does it appear in the source code.

I think it is really confusing that vscode's pylance/pyright can resolve typing_extensions.Required and typing_extensions.NotRequired, even though it isn't implemented in the module.

As a workaround you could try to replace from typing_extensions import Required with

try:
    from typing_extensions import Required
except ImportError:
    from typing import Generic, TypeVar

    T = TypeVar("T")

    class Required(Generic[T]):
        pass

CodePudding user response:

Try to upgrade your typing-extensions..

pip install typing-extensions --upgrade
  • Related