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))

data_scaled = RobustScaler().fit_transform(df.to_numpy())
df_scaled = pd.DataFrame(data_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 your own code? Could you try renaming that file to something else?

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

No is from the Python library 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/", line 407, in _suppress_matplotlib_deprecation_warning
    with warnings.catch_warnings():File "/home/matt/code/monash/autumn/env/lib/python3.6/", line 437, in __init__
    self._module = sys.modules['warnings'] if module is None else module

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

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]

Hi @matthew_segal, welcome to the community!

Thanks for taking a look at this bug. Would you mind submitting this as a PR to

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.


Raised as an issue here:

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

1 Like


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

import warnings


1 Like