Error in torch with streamlit

This is the error which is a conflict between torch and streamlit:

2025-01-19 07:06:19.117 Examining the path of torch.classes raised: Tried to instantiate class 'path.path’, but it does not exist! Ensure that it is registered via torch::class

Steps to reproduce:
python 3.10.16
streamlit 1.41.1
torch 2.5.1

The code:

# test1.py
import torch
print(torch.__version__)
print(torch.cuda.is_available())

python test1.py works fine.

The code

# test2.py
import torch
import streamlit as st
print(torch.__version__)
print(torch.cuda.is_available())
st.write("hello")

streamlit run test2.py

gives the error:

2025-01-19 07:06:19.117 Examining the path of torch.classes raised: Tried to instantiate class 'path.path’, but it does not exist! Ensure that it is registered via torch::class

Any view how to fix?

2 Likes

I have the same issue when using langchain with streamlit

I have same issue.

Even I see the same error.

Examining the path of torch.classes raised: Tried to instantiate class 'path.path’, but it does not exist! Ensure that it is registered via torch::class

In case it helps someone, I managed to fix this issue by doing the following:

import os
import torch
import streamlit

torch.classes.__path__ = [os.path.join(torch.__path__[0], torch.classes.__file__)] 

# or simply:
torch.classes.__path__ = []
1 Like