Module not found ReportThread

There are examples everywhere using streamlit.ReportThread, however streamlit.ReportThread throws a module not found error on multiple machines. streamlit.report_thread does exist but none of the functions work if i use streamlit.report_thread. How do i fix this error? do i need a specific version of streamlit so that streamlit.ReportThread does exist and the examples work?

Thanks for the help!!

Here is an example that previously worked but no longer does because ReportThread is not found

import streamlit.ReportThread as ReportThread

To add a little more to this. streamlit version 0.63.0 works with
from streamlit import ReportThread

If ReportThread was changed to streamlit.report_thread after version 0.63.0 then why are all examples still showing
from streamlit import ReportThread

Also, if i try to update from ReportThread to report_thread in later versions streamlit then it breaks code that used ReportThread.

2 Likes

I was running into the same problem and I found this gist to be helpful: DO NOT USE!!! Try st.session_state instead. · GitHub

Particularly this section for this specific problem:

try:
    import streamlit.ReportThread as ReportThread
    from streamlit.server.Server import Server
except Exception:
    # Streamlit >= 0.65.0
    import streamlit.report_thread as ReportThread
    from streamlit.server.server import Server

But I also found this section useful and related:

s = session_info.session
if (
    # Streamlit < 0.54.0
    (hasattr(s, '_main_dg') and s._main_dg == ctx.main_dg)
    or
    # Streamlit >= 0.54.0
    (not hasattr(s, '_main_dg') and s.enqueue == ctx.enqueue)
    or
    # Streamlit >= 0.65.2
    (not hasattr(s, '_main_dg') and s._uploaded_file_mgr == ctx.uploaded_file_mgr)
):
    this_session = s
1 Like

Hello @Codeguyross,

Most of the examples were made before Streamlit’s code refactoring, prior version 0.65.
Some gist (and comments below them) provide patches for further Streamlit version. @Ben_Leathers’s reply should work in your case.

And really soon we’ll have an official verison for this I think :wink:

1 Like

After PR Feature/report refactor by AnOctopus · Pull Request #4141 · streamlit/streamlit · GitHub
from streamlit.report_thread import add_report_ctx is changed to
from streamlit.script_run_context import add_script_run_ctx

To adopt to above change below worked for me
from streamlit.script_run_context import add_script_run_ctx as add_report_ctx

Also this is what new code needs to look like if you’re using this to get session_id for extracting headers or whatnot.

-from streamlit.report_thread import get_report_ctx
+fro streamlit.script_run_context import add_script_run_ctx
...
-    session_id = get_report_ctx().session_id
+    session_id = add_script_run_ctx().streamlit_script_run_ctx.session_id
2 Likes

Looks like the syntax changed again. If you are running into a ModuleNotFoundError :

ModuleNotFoundError: No module named 'streamlit.script_run_context'

The new syntax is the following:

-from streamlit.script_run_context import add_script_run_ctx
+from streamlit.scriptrunner import add_script_run_ctx
2 Likes