ModuleNotFoundError: No module named 'optree'

import tensorflow as tf
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input,LSTM,Dense

this issue is arising when im using kaggle tpu

ModuleNotFoundError Traceback (most recent call last)
Cell In[4], line 2
1 import tensorflow as tf
----> 2 from tensorflow.keras.models import Model
3 from tensorflow.keras.layers import Input,LSTM,Dense
4 from sklearn.feature_extraction.text import CountVectorizer

File /usr/local/lib/python3.10/site-packages/keras/_tf_keras/keras/init.py:8
1 “”“DO NOT EDIT.
2
3 This file was autogenerated. Do not edit it by hand,
4 since your modifications would be overwritten.
5 “””
----> 8 from keras import activations
9 from keras import applications
10 from keras import callbacks

File /usr/local/lib/python3.10/site-packages/keras/init.py:8
1 “”“DO NOT EDIT.
2
3 This file was autogenerated. Do not edit it by hand,
4 since your modifications would be overwritten.
5 “””
----> 8 from keras import _tf_keras
9 from keras import activations
10 from keras import applications

File /usr/local/lib/python3.10/site-packages/keras/_tf_keras/init.py:1
----> 1 from keras._tf_keras import keras

File /usr/local/lib/python3.10/site-packages/keras/_tf_keras/keras/init.py:8
1 “”“DO NOT EDIT.
2
3 This file was autogenerated. Do not edit it by hand,
4 since your modifications would be overwritten.
5 “””
----> 8 from keras import activations
9 from keras import applications
10 from keras import callbacks

File /usr/local/lib/python3.10/site-packages/keras/activations/init.py:8
1 “”“DO NOT EDIT.
2
3 This file was autogenerated. Do not edit it by hand,
4 since your modifications would be overwritten.
5 “””
----> 8 from keras.src.activations import deserialize
9 from keras.src.activations import get
10 from keras.src.activations import serialize

File /usr/local/lib/python3.10/site-packages/keras/src/init.py:1
----> 1 from keras.src import activations
2 from keras.src import applications
3 from keras.src import backend

File /usr/local/lib/python3.10/site-packages/keras/src/activations/init.py:3
1 import types
----> 3 from keras.src.activations.activations import elu
4 from keras.src.activations.activations import exponential
5 from keras.src.activations.activations import gelu

File /usr/local/lib/python3.10/site-packages/keras/src/activations/activations.py:1
----> 1 from keras.src import backend
2 from keras.src import ops
3 from keras.src.api_export import keras_export

File /usr/local/lib/python3.10/site-packages/keras/src/backend/init.py:10
7 import torch
9 from keras.src.backend.common.dtypes import result_type
—> 10 from keras.src.backend.common.keras_tensor import KerasTensor
11 from keras.src.backend.common.keras_tensor import any_symbolic_tensors
12 from keras.src.backend.common.keras_tensor import is_keras_tensor

File /usr/local/lib/python3.10/site-packages/keras/src/backend/common/keras_tensor.py:2
1 from keras.src.api_export import keras_export
----> 2 from keras.src.utils import tree
3 from keras.src.utils.naming import auto_name
6 @keras_export(“keras.KerasTensor”)
7 class KerasTensor:

File /usr/local/lib/python3.10/site-packages/keras/src/utils/tree.py:5
2 import collections.abc
3 import types
----> 5 import optree
7 from keras.src.api_export import keras_export
8 from keras.src.backend.config import backend

ModuleNotFoundError: No module named ‘optree’

Hi @javvadqamar, This ModuleNotFoundError occurs when you try to import a module which is not present. please try to install the optree using pip install optree. Thank You.

@Kiran_Sai_Ramineni thank you for replying
optree is already installed and the code is working fine when I’m using GPU accelerator
but its not working on TPU accelerator. I’ve tried uninstalling and reinstalling tensorflow(2.15.0) and keras (3.1.1) and its still not working.
when i use “from tensorflow import keras” this line works but on next lines it says cannot find keras please check it is installed or not when checking its installed

edit: nevermind ive reinstalled everything from scratch now it works