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

1 Like

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.

I’m facing the same issue. Tried VS Code and Spyder, both resulted in the same error.

Did you have any luck?

No I have not :-(. The walk around I am currently using is temporary commenting the session state commands for debugging of the rest of the code… Not the elegant solution at all.

1 Like

I’m also having the same issue just recently also . Using VSCode (both on Mac and Windows) . And the work around I do is the same, commenting out the session state commands, run it and once the browser is up uncomment it and it’s working fine.
I did not have this problem a few weeks back. I tried to use an older version of the python extension (read somewhere that I may work) but still not working. Hope I can find a solution for this soon…

created a new environment and working now… …https://docs.streamlit.io/library/get-started/installation#install-streamlit-on-macoslinux

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