Conda ImportError when trying to run python file

conda
error
terminal
Published

May 14, 2024

Update typing_extensions when getting ImportError

Traceback (most recent call last):
...
from typing_extensions import TypeAlias # Python 3.10+ ImportError: cannot import name 'TypeAlias' from 'typing_extensions' (/opt/homebrew/Caskroom/miniforge/base/envs/my_env/lib/python3.10/site-packages/typing_extensions.py)

Steps to reproduce error

  1. Open terminal
  2. Create requirements.txt in current working directory
# requirements.txt
typing-extensions ==3.7.4
  1. Create new environment with python=3.10, install pip, install required libraries from requirements.txt using pip. Here, our environment is called my_env.
conda create -n myenv python=3.10
conda install pip
pip install -r requirements.txt
  1. Create a dummy python file called run.py in current working directory
# run.py
print('hello world!')
  1. Run run.py from terminal
python run.py

How to fix it

Upgrade typing_extensions by:

pip install typing_extensions==4.7.1 --upgrade