Session state initialization error

The session state initialization error shows up, even when the session state is properly initialized.
The code taken ‘as is’ from the Example 1 Streamlit Docs

import streamlit as st

st.title(‘Counter Example’)
if ‘count’ not in st.session_state:
st.session_state.count = 0

increment = st.button(‘Increment’)
if increment:
st.session_state.count += 1

st.write('Count = ', st.session_state.count)

Returns the following error
AttributeError: st.session_state has no attribute “count”. Did you forget to initialize it? More info: Add statefulness to apps - Streamlit Docs

The same is happening with my own code. I see similar questions in the forum, not yet addressed, so decided to raise a new one. Would appreciate any help… Thank you.

Streamlit ver. 1.4

I do:

if 'count' not in st.session_state:
   st.session_state['count'] = 0

Have you tried that?

Dinesh

Yes, I have tried that too with no success.

What version of Streamlit?

Dinesh

HI @blop,

I copy-pasted your code and it worked fine for me using both Streamlit v1.3 and v1.4.

can you tell us more about your environment (computer OS, streamlit, python and pip/conda version and your browser type and version)?

Can you take a screenshot of the error and terminal output of streamlit --version?

import streamlit as st

st.title("Counter Example")

if "count" not in st.session_state:
    st.session_state.count = 0

increment = st.button("Increment")
if increment:
    st.session_state.count += 1

st.write('Count = ', st.session_state.count)

Screen Shot 2022-01-14 at 2.54.53 PM

Happy Streamlit-ing!
Marisa

Here they are



OS: Window 10. Browser: Chromium.
Interestingly enough, the code does work in the webapp, but not in the IDE.

hi @blop,

what do you mean by this:

I mean that execution in the IDE, i.e. Spyder in my case, gives the error, as on the screenshot above. However, in the browser the webapp works just fine.

Hi @blop

Spyder is known to sometimes give errors that don’t happen in other IDEs.

I’ve faced Module errors that only occur in Spyder. The same code worked flawlessly in vscode.

Try vscode. The error may disappear.

Cheers

Thanks to everyone trying to help!
The issue with executing the Streamlit code in an IDE is not IDE-dependent (also tried VSCode and Jupyter). It seems to be rather associated with the session state processing not done by IDE, as noted here. However, I am still struggling to adapt the proposed solution to the simple example code, and any suggestions would be appreciated.
Also, the general topic of using IDEs for Streamlit code development and debugging does not seem to be well covered in the documentation.