Last updated: Apr 10, 2024
Reading timeยท6 min
The "ImportError: cannot import name 'Required' from 'typing_extensions'"
occurs when we have an outdated version of the typing-extensions
module.
To solve the error, upgrade typing-extensions
by running the
pip install typing-extensions --upgrade
command.
ImportError: cannot import name 'Required' from 'typing_extensions' (/home/borislav/Desktop/bobbyhadz_python/venv2/lib/python3.10/site-packages/typing_extensions.py) ImportError: cannot import name 'NotRequired' from 'typing_extensions' (/home/borislav/Desktop/bobbyhadz_python/venv2/lib/python3.10/site-packages/typing_extensions.py)
The Required
and NotRequired
classes have been added to the
typing-extensions
module starting version 4.0.0
.
Open your terminal and run the following command to upgrade the
typing-extensions
module.
pip install typing-extensions --upgrade pip3 install typing-extensions --upgrade # ๐๏ธ If you don't have pip in PATH environment variable python -m pip install typing-extensions --upgrade python3 -m pip install typing-extensions --upgrade # ๐๏ธ py alias (Windows) py -m pip install typing-extensions --upgrade # ๐๏ธ For Anaconda conda install -c conda-forge typing-extensions conda update typing-extensions # ๐๏ธ For Jupyter Notebook !pip install typing-extensions --upgrade
The --upgrade option upgrades the specified package to the newest available version.
Required
and NotRequired
classesOnce you upgrade your typing-extensions
module, you should be able to import
the Required
and NotRequired
classes as follows.
from typing_extensions import Required, NotRequired print(Required) print(NotRequired)
You can use the pip show typing-extensions
command to check your version of
the module.
pip show typing-extensions pip3 show typing-extensions
typing
module insteadIf you use a version of Python greater than or equal to 3.11
, you can also use
the built-in typing module to
import the
Required class.
# โ For Python versions >= 3.11 from typing import Required, NotRequired print(Required) print(NotRequired)
You can check your Python version with the python --version
command.
python --version python3 --version
You can upgrade your Python version by downloading the installer from the official python.org website and running it.
Make sure to tick the following options if you get prompted:
If none of the suggestions helped, you can try to upgrade all packages in your environment.
The most straightforward way to upgrade all outdated packages is to use a Python script.
import pkg_resources from subprocess import call packages = [dist.project_name for dist in pkg_resources.working_set] call("pip install --upgrade " + ' '.join(packages), shell=True)
main.py
and run the file with python main.py
to upgrade all of the outdated packages.Here are alternative commands you can use to upgrade all outdated packages.
# ๐๏ธ macOS or Linux pip install -U `pip list --outdated | awk 'NR>2 {print $1}'` # ๐๏ธ Windows for /F "delims= " %i in ('pip list --outdated') do pip install -U %i
If you use a requirements.txt file, you can update it with the following command.
pip freeze > requirements.txt
The "ImportError: cannot import name 'ParamSpec' from 'typing_extensions'"
occurs when we have an outdated version of the typing-extensions
module.
To solve the error, upgrade typing-extensions
by running the
pip install typing-extensions --upgrade
command.
ImportError: cannot import name 'ParamSpec' from 'typing_extensions' (/home/borislav/Desktop/bobbyhadz_python/venv2/lib/python3.10/site-packages/typing_extensions.py)
Open your terminal and run the following command to upgrade the
typing-extensions
module.
pip install typing-extensions --upgrade pip3 install typing-extensions --upgrade # ๐๏ธ If you don't have pip in PATH environment variable python -m pip install typing-extensions --upgrade python3 -m pip install typing-extensions --upgrade # ๐๏ธ py alias (Windows) py -m pip install typing-extensions --upgrade # ๐๏ธ For Anaconda conda install -c conda-forge typing-extensions conda update typing-extensions # ๐๏ธ For Jupyter Notebook !pip install typing-extensions --upgrade
The --upgrade option upgrades the specified package to the newest available version.
typing-extensions
module, you should be able to import the ParamSpec
class as follows.from typing_extensions import ParamSpec print(ParamSpec)
If you use the fastapi package and the error persists, try reinstalling it.
pip uninstall fastapi typing-extensions -y pip install fastapi typing-extensions --no-cache-dir pip3 uninstall fastapi typing-extensions -y pip3 install fastapi typing-extensions --no-cache-dir python -m pip uninstall fastapi typing-extensions -y python -m pip install fastapi typing-extensions --no-cache-dir python3 -m pip uninstall fastapi typing-extensions -y python3 -m pip install fastapi typing-extensions --no-cache-dir # ๐๏ธ For Anaconda conda remove typing-extensions conda remove fastapi conda install -c conda-forge typing-extensions conda install -c conda-forge fastapi # ๐๏ธ For Jupyter Notebook !pip uninstall fastapi typing-extensions -y !pip install fastapi typing-extensions --no-cache-dir
You can read more about pip's --no-cache-dir
option in
the following article.
If you use a version of Python greater than or equal to 3.10
, you can also use
the built-in typing module to
import the
ParamSpec
class.
# โ For Python versions >= 3.10 from typing import ParamSpec print(ParamSpec)
You can check your Python version with the python --version
command.
python --version python3 --version
You can upgrade your Python version by downloading the installer from the official python.org website and running it.
Make sure to tick the following options if you get prompted:
If none of the suggestions helped, you can try to upgrade all packages in your environment.
The most straightforward way to upgrade all outdated packages is to use a Python script.
import pkg_resources from subprocess import call packages = [dist.project_name for dist in pkg_resources.working_set] call("pip install --upgrade " + ' '.join(packages), shell=True)
main.py
and run the file with python main.py
to upgrade all of the outdated packages.Here are alternative commands you can use to upgrade all outdated packages.
# ๐๏ธ macOS or Linux pip install -U `pip list --outdated | awk 'NR>2 {print $1}'` # ๐๏ธ Windows for /F "delims= " %i in ('pip list --outdated') do pip install -U %i
If you use a requirements.txt
file, you can update it with the following
command.
pip freeze > requirements.txt
The "ImportError: cannot import name 'TypeGuard' from 'typing_extensions'"
occurs when we have an outdated version of the typing-extensions
module.
To solve the error, upgrade typing-extensions
by running the
pip install typing-extensions --upgrade
command.
ImportError: cannot import name 'TypeGuard' from 'typing_extensions' (/home/borislav/Desktop/bobbyhadz_python/venv2/lib/python3.10/site-packages/typing_extensions.py)
Open your terminal and run the following command to upgrade the
typing-extensions
module.
pip install typing-extensions --upgrade pip3 install typing-extensions --upgrade # ๐๏ธ If you don't have pip in PATH environment variable python -m pip install typing-extensions --upgrade python3 -m pip install typing-extensions --upgrade # ๐๏ธ py alias (Windows) py -m pip install typing-extensions --upgrade # ๐๏ธ For Anaconda conda install -c conda-forge typing-extensions conda update typing-extensions # ๐๏ธ For Jupyter Notebook !pip install typing-extensions --upgrade
The --upgrade option upgrades the specified package to the newest available version.
typing-extensions
module, you should be able to import TypeGuard
as follows.from typing_extensions import TypeGuard print(TypeGuard)
You can use the pip show typing-extensions
command to check your version of
the module.
pip show typing-extensions pip3 show typing-extensions
If you use a version of Python greater than or equal to 3.10
, you can also use
the built-in typing module to
import the
TypeGuard
class.
# โ For Python versions >= 3.10 from typing import TypeGuard print(TypeGuard)
You can check your Python version with the python --version
command.
python --version python3 --version
You can upgrade your Python version by downloading the installer from the official python.org website and running it.
Make sure to tick the following options if you get prompted:
If none of the suggestions helped, you can try to upgrade all packages in your environment.
The most straightforward way to upgrade all outdated packages is to use a Python script.
import pkg_resources from subprocess import call packages = [dist.project_name for dist in pkg_resources.working_set] call("pip install --upgrade " + ' '.join(packages), shell=True)
main.py
and run the file with python main.py
to upgrade all of the outdated packages.Here are alternative commands you can use to upgrade all outdated packages.
# ๐๏ธ macOS or Linux pip install -U `pip list --outdated | awk 'NR>2 {print $1}'` # ๐๏ธ Windows for /F "delims= " %i in ('pip list --outdated') do pip install -U %i
If you use a requirements.txt
file, you can update it with the following
command.
pip freeze > requirements.txt