Streamlit: Streamlit failed to hash an object

Created on 2 Oct 2019  路  17Comments  路  Source: streamlit/streamlit

I am trying to get the DataFrame Demo from streamlit hello to work. I am running this using Anaconda and Python 3.6.

I get the following error in the browser:

Streamlit cannot hash an object of type <class 'os._Environ'>.,

More information: to prevent unexpected behavior, Streamlit tries to detect mutations in cached objects so it can alert the user if needed. However, something went wrong while performing this check.

Please file a bug.

To stop this warning from showing in the meantime, try one of the following:

Preferred: modify your code to avoid using this type of object.
Or add the argument ignore_cache=True to the st.cache decorator.

I get a bunch of additional errors on Streamlit failed to hash an object of type <class 'function'>.,

And then this error blob:

TypeError: object supporting the buffer API required
File "/Users/lberndt/anaconda/envs/python3/lib/python3.6/site-packages/streamlit/ScriptRunner.py", line 306, in _run_script exec(code, module.__dict__)
File "/Users/lberndt/anaconda/envs/python3/lib/python3.6/site-packages/streamlit/hello.py", line 334, in <module> run()
File "/Users/lberndt/anaconda/envs/python3/lib/python3.6/site-packages/streamlit/hello.py", line 308, in run demo()
File "/Users/lberndt/anaconda/envs/python3/lib/python3.6/site-packages/streamlit/hello.py", line 247, in data_frame_demo df = get_UN_data()
File "/Users/lberndt/anaconda/envs/python3/lib/python3.6/site-packages/streamlit/caching.py", line 544, in wrapped_func return get_or_set_cache()
File "/Users/lberndt/anaconda/envs/python3/lib/python3.6/site-packages/streamlit/caching.py", line 509, in get_or_set_cache code_hasher.update(func)
File "/Users/lberndt/anaconda/envs/python3/lib/python3.6/site-packages/streamlit/hashing.py", line 185, in update self._update(self.hasher, obj, context)
File "/Users/lberndt/anaconda/envs/python3/lib/python3.6/site-packages/streamlit/hashing.py", line 217, in _update hasher.update(b)
bug cache

Most helpful comment

same here, macos, conda, python 3.7.4

All 17 comments

Thanks for letting us know @robotastic! We're looking into this!

I am seeing this too on my platform, ubuntu 18 python 3.6. It is the mapping demo. The plotting demo works fine, but none of the others do.

same here, macos, conda, python 3.7.4

I backed up to v0.46.0 pip install streamlit==0.46.0 and I don't get that same error. The demo page is a lot different but I was able to run the udacity self driving car tutorial without issue. So this problem seems confined to the latest release.

Don't know if I should open a new bug or not, but I have a similar issue with my own code for:
<class 'tuple'>, <class 'list'>, <class 'pathlib.PosixPath'>.
Downgrading to 0.36 did not solve the issue...

Hi @takacsd, this may be covered by this, but if you have some repro instructions I can verify it is actually the same bug.

I have the same issue with a Mac OS (10.14.6) and Python 3.6. Downgrading to 0.46 didn't solve the issue

I have the same issue as well with a Mac OS (10.14.6), Python 3.7, streamlit (0.47.4). Very willing to help through a call or something like that :)

My debugging information:

TypeError: object supporting the buffer API required
File "/sandbox/streamlit-demo/venv/lib/python3.7/site-packages/streamlit/ScriptRunner.py", line 306, in _run_script exec(code, module.__dict__)
File "/sandbox/streamlit-demo/venv/lib/python3.7/site-packages/streamlit/hello.py", line 334, in <module> run()
File "/sandbox/streamlit-demo/venv/lib/python3.7/site-packages/streamlit/hello.py", line 308, in run demo()
File "/sandbox/streamlit-demo/venv/lib/python3.7/site-packages/streamlit/hello.py", line 247, in data_frame_demo df = get_UN_data()
File "/sandbox/streamlit-demo/venv/lib/python3.7/site-packages/streamlit/caching.py", line 544, in wrapped_func return get_or_set_cache()
File "/sandbox/streamlit-demo/venv/lib/python3.7/site-packages/streamlit/caching.py", line 509, in get_or_set_cache code_hasher.update(func)
File "/sandbox/streamlit-demo/venv/lib/python3.7/site-packages/streamlit/hashing.py", line 185, in update self._update(self.hasher, obj, context)
File "/sandbox/streamlit-demo/venv/lib/python3.7/site-packages/streamlit/hashing.py", line 217, in _update hasher.update(b)

I have the same problem with streamlit 0.47.4, Python 3.6.8, Ubuntu 18.04 running inside WSL@Windows10.

Workaround:
Commenting out @st.cache in the following piece of code solved the issue for me:

# This is the main app app itself, which appears when the user selects "Run the app".
def run_the_app():
    # To make Streamlit fast, st.cache allows us to reuse computation across runs.
    # In this common pattern, we download data from an endpoint only once.
    #@st.cache
    def load_metadata(url):
        return pd.read_csv(url)

Commenting out the @st.cache decorators in hello.py (lines 69 and 240) allowed the mapping and dataframe demos to work for me (release 0.47.4).

Thanks @ahmedahmedov

Hello again, I just found something interesting. If I run the Udacity demo with:

streamlit run https://raw.githubusercontent.com/streamlit/demo-self-driving/master/app.py

It runs just fine!

But if I download the file app.py to the local filesystem and run:

streamlit run app.py

Then it fails with four complaints Streamlit failed to hash an object of type <class 'function'>.... in yellow blocks followed by the error message:

TypeError: object supporting the buffer API required
File "/mnt/c/zzz/streamlit_udacity_demo/venv/lib/python3.6/site-packages/streamlit/ScriptRunner.py", line 306, in _run_script exec(code, module.__dict__)
File "/mnt/c/zzz/streamlit_udacity_demo/app.py", line 296, in <module> main()
File "/mnt/c/zzz/streamlit_udacity_demo/app.py", line 45, in main run_the_app()
File "/mnt/c/zzz/streamlit_udacity_demo/app.py", line 109, in run_the_app metadata = load_metadata(os.path.join(DATA_URL_ROOT, "labels.csv.gz"))
File "/mnt/c/zzz/streamlit_udacity_demo/venv/lib/python3.6/site-packages/streamlit/caching.py", line 544, in wrapped_func return get_or_set_cache()
File "/mnt/c/zzz/streamlit_udacity_demo/venv/lib/python3.6/site-packages/streamlit/caching.py", line 509, in get_or_set_cache code_hasher.update(func)
File "/mnt/c/zzz/streamlit_udacity_demo/venv/lib/python3.6/site-packages/streamlit/hashing.py", line 185, in update self._update(self.hasher, obj, context)
File "/mnt/c/zzz/streamlit_udacity_demo/venv/lib/python3.6/site-packages/streamlit/hashing.py", line 217, in _update hasher.update(b) 

@lcerman: I believe the issue you're seeing is this one, which has a different cause.

@lcerman: I believe the issue you're seeing is this one, which has a different cause.

Interestingly, the bug #167 exhibits wrong behavior of the remote code, while in my case the remote code works fine, but the local code fails (this is not only problem of the Udacity demo, it fails for every cached function in the other tutorials or in my own code).

The fix landed on develop a couple of days ago.

But since this bug is so popular, I'll leave this open until the release goes out. This way other people are more likely to find it.

Released as 0.48.1

More info: When you st.cache a function, Streamlit's caching mechanism traverses all function calls inside it to track whether the called function's body was edited since the cached function was last cached. And Streamlit already ignored "library" functions, since those can be assumed to be unmodified.

However, the way we defined library functions was "functions that were defined outside your current working directory its subfolders". But this broke whenever your Python environment was placed _inside_ the cwd!

So since version 0.48.1 we define "library function" as any function that was defined:

  • Outside your current working directory its subfolders
  • In a blacklisted folder:

    • The default blacklist is:

      "**/.*", "**/anaconda", "**/anaconda2", "**/anaconda3", "**/miniconda", "**/miniconda2", "**/miniconda3", "**/venv", "**/virtualenv", "**/pyenv",

    • Plus anything you specify with the Streamlit config option server.folderWatchBlacklist.

In addition, we also added this explanation to our error message:

This error can occur when your virtual environment lives in the same
folder as your project, since that makes it hard for Streamlit to
understand which files it should check. If you think that's what caused
this, please add the following to ~/.streamlit/config.toml:

[server] folderWatchBlacklist = ['foldername']

...where foldername is the relative or absolute path to the folder
where you put your virtual environment.


Of course, this still doesn't solve the underlying problem of why those library functions were not hashable in the first place. We are working on that right now, but since there are a few different cases where things may not be hashable, I'd rather create one bug for each case than have a long-stanging bug with lots of mixed conversations.

Also, for almost everyone who reported this issue, the script that caused the bug was streamlit hello (which we know is 100% hashable!) and the tracebacks provided show the error was always due to library functions not being hashable (which is what we fixed now). So I'll close this bug now.

Have tested the new version and I confirm its seems fixed, thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pybokeh picture pybokeh  路  17Comments

tvst picture tvst  路  31Comments

andfanilo picture andfanilo  路  23Comments

monchier picture monchier  路  20Comments

DylanModesitt picture DylanModesitt  路  16Comments