Search code examples
python-3.xvisual-studio-codekerasdeep-learningimporterror

optree is actually installed but when running "from keras.models import Sequential" it still throws ImportError asking me to install optree, why?


I'm trying to use VS Code to start a small data science project, I'm currently having issues while importing keras by running the following lines:

LSTM and GRU project

Dependencies

import pandas as pd
import matplotlib as mpl
from sklearn.preprocessing import MinMaxScaler
from keras.models import Sequential
from keras.layers import LSTM, Dropout, Dense

Traceback goes as follow:

ImportError                               Traceback (most recent call last) ~\AppData\Local\Temp\ipykernel_21244\449270897.py in <cell line: 6>()
      4 import matplotlib as mpl
      5 from sklearn.preprocessing import MinMaxScaler
----> 6 from keras.models import Sequential
      7 from keras.layers import LSTM, Dropout, Dense

c:\Users\ResetStoreX\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\__init__.py in <module>
      8 
      9 # Import everything from /api/ into keras.
---> 10 from keras.api import *  # noqa: F403
     11 from keras.api import __version__  # Import * ignores names start with "_".
     12 

c:\Users\ResetStoreX\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\api\__init__.py in <module>
      6 
      7 
----> 8 from keras.api import activations
      9 from keras.api import applications
     10 from keras.api import backend

c:\Users\ResetStoreX\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\api\activations\__init__.py in <module>
      5 """
      6  ...
---> 10     raise ImportError(
     11         "To use Keras, you need to have `optree` installed. "
     12         "Install it via `pip install optree`"

ImportError: To use Keras, you need to have `optree` installed. Install it via `pip install optree` Output is truncated. View as a scrollable element or open in a text editor. Adjust cell output settings...

I decided to run in the terminal commands like pip install keras --upgrade --user to check if any of the dependencies above are indeed not installed, the output is quite disappointing since it states that every single dependency used in this project has its requirements already satisfied, see:

C:\Users\ResetStoreX\Downloads\Cryptobote 2024> pip install numpy --upgrade --user
Requirement already satisfied: numpy in c:\users\resetstorex\appdata\local\programs\python\python39\lib\site-packages (1.26.4)
WARNING: There was an error checking the latest version of pip.
C:\Users\ResetStoreX\Downloads\Cryptobote 2024> pip install optree --upgrade --user
Requirement already satisfied: optree in c:\users\resetstorex\appdata\local\programs\python\python39\lib\site-packages (0.11.0)
Requirement already satisfied: typing-extensions>=4.0.0 in c:\users\resetstorex\appdata\local\programs\python\python39\lib\site-packages (from optree) (4.2.0)
WARNING: There was an error checking the latest version of pip.
C:\Users\ResetStoreX\Downloads\Cryptobote 2024> pip install keras --upgrade --user 
Requirement already satisfied: keras in c:\users\resetstorex\appdata\local\programs\python\python39\lib\site-packages (3.3.3)
Requirement already satisfied: numpy in c:\users\resetstorex\appdata\local\programs\python\python39\lib\site-packages (from keras) (1.26.4)
Requirement already satisfied: optree in c:\users\resetstorex\appdata\local\programs\python\python39\lib\site-packages (from keras) (0.11.0)
Requirement already satisfied: h5py in c:\users\resetstorex\appdata\local\programs\python\python39\lib\site-packages (from keras) (3.11.0)
Requirement already satisfied: ml-dtypes in c:\users\resetstorex\appdata\local\programs\python\python39\lib\site-packages (from keras) (0.4.0)
Requirement already satisfied: absl-py in c:\users\resetstorex\appdata\local\programs\python\python39\lib\site-packages (from keras) (2.1.0)
Requirement already satisfied: rich in c:\users\resetstorex\appdata\local\programs\python\python39\lib\site-packages (from keras) (13.3.1)
Requirement already satisfied: typing-extensions>=4.0.0 in c:\users\resetstorex\appdata\local\programs\python\python39\lib\site-packages (from optree->keras) (4.2.0)
Requirement already satisfied: pygments<3.0.0,>=2.14.0 in c:\users\resetstorex\appdata\local\programs\python\python39\lib\site-packages (from rich->keras) (2.14.0)
Requirement already satisfied: mdurl~=0.1 in c:\users\resetstorex\appdata\local\programs\python\python39\lib\site-packages (from markdown-it-py<3.0.0,>=2.1.0->rich->keras) (0.1.2)
WARNING: There was an error checking the latest version of pip.
C:\Users\ResetStoreX\Downloads\Cryptobote 2024> pip install pandas --upgrade --user 
Requirement already satisfied: pandas in c:\users\resetstorex\appdata\local\programs\python\python39\lib\site-packages (2.2.2)
Requirement already satisfied: python-dateutil>=2.8.2 in c:\users\resetstorex\appdata\roaming\python\python39\site-packages (from pandas) (2.8.2)
Requirement already satisfied: tzdata>=2022.7 in c:\users\resetstorex\appdata\local\programs\python\python39\lib\site-packages (from pandas) (2024.1)
Requirement already satisfied: pytz>=2020.1 in c:\users\resetstorex\appdata\roaming\python\python39\site-packages (from pandas) (2021.3)
Requirement already satisfied: numpy>=1.22.4 in c:\users\resetstorex\appdata\local\programs\python\python39\lib\site-packages (from pandas) (1.26.4)
Requirement already satisfied: six>=1.5 in c:\users\resetstorex\appdata\roaming\python\python39\site-packages (from python-dateutil>=2.8.2->pandas) (1.16.0)
WARNING: There was an error checking the latest version of pip.
C:\Users\ResetStoreX\Downloads\Cryptobote 2024> pip install matplotlib --upgrade --user 
Requirement already satisfied: matplotlib in c:\users\resetstorex\appdata\local\programs\python\python39\lib\site-packages (3.8.4)
Requirement already satisfied: packaging>=20.0 in c:\users\resetstorex\appdata\roaming\python\python39\site-packages (from matplotlib) (24.0)
Requirement already satisfied: importlib-resources>=3.2.0 in c:\users\resetstorex\appdata\local\programs\python\python39\lib\site-packages (from matplotlib) (6.4.0)
Requirement already satisfied: numpy>=1.21 in c:\users\resetstorex\appdata\local\programs\python\python39\lib\site-packages (from matplotlib) (1.26.4)
Requirement already satisfied: fonttools>=4.22.0 in c:\users\resetstorex\appdata\local\programs\python\python39\lib\site-packages (from matplotlib) (4.33.3)
Requirement already satisfied: kiwisolver>=1.3.1 in c:\users\resetstorex\appdata\local\programs\python\python39\lib\site-packages (from matplotlib) (1.4.2)
Requirement already satisfied: pillow>=8 in c:\users\resetstorex\appdata\local\programs\python\python39\lib\site-packages (from matplotlib) (8.4.0)
Requirement already satisfied: cycler>=0.10 in c:\users\resetstorex\appdata\local\programs\python\python39\lib\site-packages (from matplotlib) (0.11.0)
Requirement already satisfied: python-dateutil>=2.7 in c:\users\resetstorex\appdata\roaming\python\python39\site-packages (from matplotlib) (2.8.2)
Requirement already satisfied: contourpy>=1.0.1 in c:\users\resetstorex\appdata\local\programs\python\python39\lib\site-packages (from matplotlib) (1.2.1)
Requirement already satisfied: pyparsing>=2.3.1 in c:\users\resetstorex\appdata\local\programs\python\python39\lib\site-packages (from matplotlib) (3.0.9)
Requirement already satisfied: zipp>=3.1.0 in c:\users\resetstorex\appdata\local\programs\python\python39\lib\site-packages (from importlib-resources>=3.2.0->matplotlib) (3.6.0)
Requirement already satisfied: six>=1.5 in c:\users\resetstorex\appdata\roaming\python\python39\site-packages (from python-dateutil>=2.7->matplotlib) (1.16.0)
WARNING: There was an error checking the latest version of pip.

My Python Interpreter is Python 3.9.6 64-bit, and I am sure all of the dependencies above were installed in its corresponding path, I tried to uninstall and reinstall keras and optree including running pip cache purge as well as rebooting the kernel, but it still throws the same error.

I also noticed that keras.models and keras.layers are underlined, and when I "hover over them" it displays:

Import "keras.models" could not be resolved Pylance(reportMissingImports)

So, I'm stuck, I would like to realize what to do to handle this?


Solution

  • ¡Solved!

    It has everything to do with typing_extensions dependency, which version was too old, I realized about it this way, I ran the following script in a Jupyter Notebook cell:

    import optree
    import keras
    print(optree.__version__)
    print(keras.__version__)
    

    Which threw:

    ---------------------------------------------------------------------------
    ImportError                               Traceback (most recent call last)
    ~\AppData\Local\Temp\ipykernel_27656\3452976656.py in <cell line: 1>()
    ----> 1 import optree
          2 import keras
          3 print(optree.__version__)
          4 print(keras.__version__)
    
    c:\Users\ResetStoreX\AppData\Local\Programs\Python\Python39\lib\site-packages\optree\__init__.py in <module>
         15 """OpTree: Optimized PyTree Utilities."""
         16 
    ---> 17 from optree import integration, typing
         18 from optree.ops import (
         19     MAX_RECURSION_DEPTH,
    
    c:\Users\ResetStoreX\AppData\Local\Programs\Python\Python39\lib\site-packages\optree\typing.py in <module>
         36     Union,
         37 )
    ---> 38 from typing_extensions import NamedTuple  # Generic NamedTuple: Python 3.11+
         39 from typing_extensions import OrderedDict  # Generic OrderedDict: Python 3.7.2+
         40 from typing_extensions import Self  # Python 3.11+
    
    ImportError: cannot import name 'NamedTuple' from 'typing_extensions' (c:\Users\ResetStoreX\AppData\Local\Programs\Python\Python39\lib\site-packages\typing_extensions.py)
    

    Searching all over stackoverflow I found this answer to a similar problem, so I followed the advice anr ran such command (pip install typing_extensions==4.7.1 --upgrade) on VS Code terminal, then I ran pip install tensorflow, and then ran pip install numpy==1.23.5, to finally reboot Python kernel to be able to import such dependencies with 0 issues and warnings 👍.