KeyError `warnings`

Hi everyone!

I have half a day streamlit experience and I have the following issue.
The first time the server runs everything looks good. But then, when I make a change and the watcher reloads the page, I get this error

The code line is like this
df = df.apply(lambda x: robust_scale(x), axis=1, raw=True)

Outside streamlit code runs fine.

Thank you in advance.

Hi @anorkos, welcome to the forum :wave:

Could you share sample code that we can copy/paste and run to reproduce your error?

@Jonathan_Rhone thank you! Of course.

import streamlit as st
import pandas as pd
import numpy as np
from sklearn.preprocessing import RobustScaler

df = pd.DataFrame(np.random.rand(10, 7))
df

data_scaled = RobustScaler().fit_transform(df.to_numpy())
df_scaled = pd.DataFrame(data_scaled)
df_scaled

Run streamlit. it should display both tables at first. Then make a change in the file, let the watcher update the page, and you should see the error.

I wasn’t able to reproduce the issue.

Could you share the following?


- Streamlit version: (get it with `$ streamlit version`)
- Python version: (get it with `$ python --version`)
- Using Conda? PipEnv? PyEnv? Pex?
- OS version:
- Browser version:

Also, is warnings.py your own code? Could you try renaming that file to something else?

Streamlit Version: 0.57.3
Python Version: 3.7.6
PipEnv
Windows 10 Pro 1909
Chrome Version 81.0.4044.113

No warnings.py is from the Python library https://docs.python.org/3/library/warnings.html. It’s inside env/Lib.

It seems that it’s a warning thrown by the scikit library. The question is, why is it handled differently by streamlit at first load and subsequent loads?

The streamlit library is doing some funny business with sys.modules

At some point “warnings” is getting deleted from sys.modules

This is the bottom of the stack trace for the same issue as OP

File "/home/matt/code/monash/autumn/env/lib/python3.6/site-packages/matplotlib/cbook/deprecation.py", line 407, in _suppress_matplotlib_deprecation_warning
    with warnings.catch_warnings():File "/home/matt/code/monash/autumn/env/lib/python3.6/warnings.py", line 437, in __init__
    self._module = sys.modules['warnings'] if module is None else module

Something in streamlit is deleting “warnings” from sys.modules

1 Like

10 bucks says it’s this line of code

I think this fixes it on my machine:

        for wm in self._watched_modules.values():
        if wm.module_name is not None and wm.module_name in sys.modules:
            if wm.module_name != "warnings":
                del sys.modules[wm.module_name]
2 Likes

Hi @matthew_segal, welcome to the community!

Thanks for taking a look at this bug. Would you mind submitting this as a PR to https://github.com/streamlit/streamlit?

Hi @randyzwitch. My fix is really a situation-specific hack that would not be acceptible in the Streamlit codebase since it does not cover all bases. I believe that more investigation of this issue will be required before implementing a fix. If it’s helpful I can move this discussion to a GitHub issue and describe my hacky fix. (and link to issue from here)

That’s fair. Moving this to an issue would be great, along with your hack, so that one of our engineers can evaluate why the line that you modified is there in the first place.

Thanks!

Raised as an issue here: http://github.com/streamlit/streamlit/issues/1430

By the way @randyzwitch the forum is blocking links to github as “spam” which is very counterproductive

1 Like

Hello!

I had the same problem here and applied the workaround below and it worked for me.

import warnings
warnings.filterwarnings(‘ignore’)

Regards.

3 Likes

Worked for me thanks a lot!!

this can be a great way to solve this issue :+1:

Hi everyone!!

I have an strange issue about Key Error: 'Warnings" as below

I tried making a chart as below and succeeded first time.

However, I revise a code and reload the page after saving a code, it happens this error as below.

I found that I can succeed to reflect a chart when I “run” through new Terminal

The sample code as below.

import streamlit as st
import numpy as np
import pandas as pd
st.title(‘Streamlit begining’)
st.write(‘DataFrame’)
df = pd.DataFrame(
np.random.rand(20, 3),
columns=[‘a’, ‘b’, ‘c’]
)
st.bar_chart(df)

Thank you in advance.

Pandas KeyError occurs when we try to access some column/row label in our DataFrame that doesn’t exist. Usually, this error occurs when you misspell a column/row name or include an unwanted space before or after the column/row name… Before doing anything with the data frame, use print(df.columns) to see dataframe column exist or not.

print(df.columns)

I was getting a similar kind of error in one of my codes. Turns out, that particular index was missing from my data frame as I had dropped the empty dataframe 2 rows. If this is the case, you can do df.reset_index(inplace=True) and the error should be resolved.