When loading a function using @st.cache i get errors

When decorating a function with @st.cache I get a INternalHashError.

Steps to reproduce

Code snippet:

import pandas as pd
import streamlit as st

@st.cache
def load_data():
    df = pd.read_csv("../data/ted.csv")
    return df

df = load_data()

If applicable, please provide the steps we should take to reproduce the error or specified behavior.

Expected behavior:

Expect to see the df

Actual behavior:

error message.

Traceback (most recent call last):
File “C:\Users\Hans\anaconda3\envs\streamlit\lib\site-packages\streamlit\hashing.py”, line 289, in to_bytes
b = b"%s:%s" % (tname, self._to_bytes(obj, context))
File “C:\Users\Hans\anaconda3\envs\streamlit\lib\site-packages\streamlit\hashing.py”, line 545, in _to_bytes
if self._file_should_be_hashed(obj.code.co_filename):
File “C:\Users\Hans\anaconda3\envs\streamlit\lib\site-packages\streamlit\hashing.py”, line 324, in _file_should_be_hashed
filepath, self._get_main_script_directory()
File “C:\Users\Hans\anaconda3\envs\streamlit\lib\site-packages\streamlit\hashing.py”, line 627, in _get_main_script_directory
main_path = main.file
AttributeError: module ‘main’ has no attribute ‘file
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File “C:\Users\Hans\anaconda3\envs\streamlit\lib\site-packages\IPython\core\interactiveshell.py”, line 3508, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File “”, line 7, in
def load_data():
File “C:\Users\Hans\anaconda3\envs\streamlit\lib\site-packages\streamlit\caching.py”, line 487, in cache
update_hash(
File “C:\Users\Hans\anaconda3\envs\streamlit\lib\site-packages\streamlit\hashing.py”, line 90, in update_hash
ch.update(hasher, val, context)
File “C:\Users\Hans\anaconda3\envs\streamlit\lib\site-packages\streamlit\hashing.py”, line 314, in update
b = self.to_bytes(obj, context)
File “C:\Users\Hans\anaconda3\envs\streamlit\lib\site-packages\streamlit\hashing.py”, line 289, in to_bytes
b = b"%s:%s" % (tname, self._to_bytes(obj, context))
File “C:\Users\Hans\anaconda3\envs\streamlit\lib\site-packages\streamlit\hashing.py”, line 364, in _to_bytes
self.update(h, item, context)
File “C:\Users\Hans\anaconda3\envs\streamlit\lib\site-packages\streamlit\hashing.py”, line 314, in update
b = self.to_bytes(obj, context)
File “C:\Users\Hans\anaconda3\envs\streamlit\lib\site-packages\streamlit\hashing.py”, line 303, in to_bytes
raise InternalHashError(e, obj)
File “C:\Users\Hans\anaconda3\envs\streamlit\lib\site-packages\streamlit\hashing.py”, line 289, in to_bytes
b = b"%s:%s" % (tname, self._to_bytes(obj, context))
File “C:\Users\Hans\anaconda3\envs\streamlit\lib\site-packages\streamlit\hashing.py”, line 545, in _to_bytes
if self._file_should_be_hashed(obj.code.co_filename):
File “C:\Users\Hans\anaconda3\envs\streamlit\lib\site-packages\streamlit\hashing.py”, line 324, in _file_should_be_hashed
filepath, self._get_main_script_directory()
File “C:\Users\Hans\anaconda3\envs\streamlit\lib\site-packages\streamlit\hashing.py”, line 627, in _get_main_script_directory
main_path = main.file
streamlit.hashing.InternalHashError: module ‘main’ has no attribute ‘file
While caching the body of load_data(), Streamlit encountered an
object of type builtins.function, which it does not know how to hash.
In this specific case, it’s very likely you found a Streamlit bug so please
[file a bug report here.]
(Sign in to GitHub · GitHub)

In the meantime, you can try bypassing this error by registering a custom
hash function via the hash_funcs keyword in @st.cache(). For example:

@st.cache(hash_funcs={builtins.function: my_hash_func})
def my_func(...):
    ...

If you don’t know where the object of type builtins.function is coming
from, try looking at the hash chain below for an object that you do recognize,
then pass that to hash_funcs instead:

Object of type builtins.function: <function load_data at 0x000002397CCB5CA0>
Object of type builtins.tuple: ('__main__', 'load_data', <function load_data at 0x000002397CCB5CA0>)

Please see the hash_funcs [documentation]
(https://streamlit.io/docs/caching.html)
for more details.

bar_chart()
Traceback (most recent call last):
File “C:\Users\Hans\anaconda3\envs\streamlit\lib\site-packages\IPython\core\interactiveshell.py”, line 3508, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File “”, line 1, in
bar_chart()
NameError: name ‘bar_chart’ is not defined

Debug info

  • Streamlit version: 0.70.0 (old version because of course I am following which uses that version)
  • Python version: (3.9`)
  • Using conda
  • OS version: windows
  • Browser version: google chrome

Requirements file

streamlit==0.70.0
numpy==1.18.5
pandas==1.1.4
matplotlib==3.3.2
seaborn==0.11.0
altair==4.1.0
bokeh==2.2.3
plotly==4.12.0
Pillow==9.5.0
python-decouple==3.3

Links

  • Link to your GitHub repo:
  • Link to your deployed app:

Additional information

If needed, add any other context about the problem here.

Hey @hans,

st.cache is deprecated – please use st.cache_data instead.

1 Like

Thanks I knew but the course I follow uses Streamlit 0.70.0 where it’s not deprecated.
So I have streamlit 0.70.0 installed in my env. still doesn’t work.

There are many, many bugs that have been fixed between version 0.70.0 and 1.23.1. It might be a bit of long shot to track down workarounds for a version so far out of date.

1 Like

Fair enough. This is some course I enrolled in Udemy. I canceled :slight_smile:

Can you share a link to the course? I’m interested in cataloging out-of-date tutorials to see where we can possibly get them updated.

Sure

Complete Guide to Data Science Applications with Streamlit is the name of the course on udemy.

Link: https://www.udemy.com/course/streamlit/

It’s the highest rated for Streamlit on there. Incroyable…roflol

regards
H

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.