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.
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)
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.
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.
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.
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…
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.