Streamlit App Requires Full Restart on Replit After Code Correction

Question:

I’m facing an issue with my Streamlit application where, after fixing a syntax error and reloading the app, I encounter a problem that necessitates a full restart of the Replit environment to resolve. This issue occurs consistently after using the Streamlit reload feature, and the usual troubleshooting steps like clearing Streamlit’s cache haven’t been effective.

The problem seems to be related to the application’s or Python interpreter’s state management upon reloading. A complete restart of the environment fixes it temporarily, but I’m looking for a more practical solution that doesn’t interrupt the development flow.

Has anyone encountered a similar issue or can offer advice on how to manage Streamlit application states more effectively on Replit? Any suggestions or workarounds would be greatly appreciated.

Thanks in advance for your help!

Repl link/Link to where the bug appears:
https://replit.com/@danielseijo1/Apolo-Hawkings

Screenshots, links, or other helpful context:

2024-02-06 21:38:23.603 Uncaught app exception
Traceback (most recent call last):
  File "/home/runner/Apolo-Hawkings/.pythonlibs/lib/python3.10/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 535, in _run_script
    exec(code, module.__dict__)
  File "/home/runner/Apolo-Hawkings/pages/01_Evaluacion_de_trabajos.py", line 75, in <module>
    response = client.chat.completions.create(model=modelo_seleccionado,
  File "/home/runner/Apolo-Hawkings/.pythonlibs/lib/python3.10/site-packages/openai/_utils/_utils.py", line 271, in wrapper
    return func(*args, **kwargs)
  File "/home/runner/Apolo-Hawkings/.pythonlibs/lib/python3.10/site-packages/openai/resources/chat/completions.py", line 661, in create
    body=maybe_transform(
  File "/home/runner/Apolo-Hawkings/.pythonlibs/lib/python3.10/site-packages/openai/_utils/_transform.py", line 74, in maybe_transform
    return transform(data, expected_type)
  File "/home/runner/Apolo-Hawkings/.pythonlibs/lib/python3.10/site-packages/openai/_utils/_transform.py", line 97, in transform
    transformed = _transform_recursive(data, annotation=cast(type, expected_type))
  File "/home/runner/Apolo-Hawkings/.pythonlibs/lib/python3.10/site-packages/openai/_utils/_transform.py", line 170, in _transform_recursive
    data = _transform_recursive(data, annotation=annotation, inner_type=subtype)
  File "/home/runner/Apolo-Hawkings/.pythonlibs/lib/python3.10/site-packages/openai/_utils/_transform.py", line 158, in _transform_recursive
    return _transform_typeddict(data, stripped_type)
  File "/home/runner/Apolo-Hawkings/.pythonlibs/lib/python3.10/site-packages/openai/_utils/_transform.py", line 216, in _transform_typeddict
    result[_maybe_transform_key(key, type_)] = _transform_recursive(value, annotation=type_)
  File "/home/runner/Apolo-Hawkings/.pythonlibs/lib/python3.10/site-packages/openai/_utils/_transform.py", line 162, in _transform_recursive
    return [_transform_recursive(d, annotation=annotation, inner_type=inner_type) for d in data]
  File "/home/runner/Apolo-Hawkings/.pythonlibs/lib/python3.10/site-packages/openai/_utils/_transform.py", line 162, in <listcomp>
    return [_transform_recursive(d, annotation=annotation, inner_type=inner_type) for d in data]
  File "/home/runner/Apolo-Hawkings/.pythonlibs/lib/python3.10/site-packages/openai/_utils/_transform.py", line 170, in _transform_recursive
    data = _transform_recursive(data, annotation=annotation, inner_type=subtype)
  File "/home/runner/Apolo-Hawkings/.pythonlibs/lib/python3.10/site-packages/openai/_utils/_transform.py", line 158, in _transform_recursive
    return _transform_typeddict(data, stripped_type)
  File "/home/runner/Apolo-Hawkings/.pythonlibs/lib/python3.10/site-packages/openai/_utils/_transform.py", line 216, in _transform_typeddict
    result[_maybe_transform_key(key, type_)] = _transform_recursive(value, annotation=type_)
  File "/home/runner/Apolo-Hawkings/.pythonlibs/lib/python3.10/site-packages/openai/_utils/_transform.py", line 173, in _transform_recursive
    if isinstance(data, pydantic.BaseModel):
  File "/home/runner/Apolo-Hawkings/.pythonlibs/lib/python3.10/site-packages/pydantic/__init__.py", line 383, in __getattr__
    module = import_module(module_name, package=package)
  File "/nix/store/xf54733x4chbawkh1qvy9i1i4mlscy1c-python3-3.10.11/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1024, in _find_and_load
  File "<frozen importlib._bootstrap>", line 170, in __enter__
  File "<frozen importlib._bootstrap>", line 185, in _get_module_lock
NameError: name '_imp' is not defined

You could run kill 1 in the shell, or put it in your program.

To clarify, put the lines in your main file:

import os
os.system('kill 1')

This will, of course, run the command whenever running the repl, but the issue with this is that your code may not run at all, since it just reloads the repl, but doesn’t run the code after that. You should run it in the Shell, not put it in the code.

Yep, but the problem is I need to do that every time I find a mistake in the code.

The advantage of Streamlit is that you can reload the files without killing the server and booting it up again. That has never happened to me with local servers or Hetzner servers.

It’s not a big deal, but I can use Replit as an IDE :smiley:

I’m getting the same error. Like danielseijo1 said, its whenever the streamlit app reloads

NameError: name '_imp' is not defined
2024-03-01 06:34:58.168 Uncaught app exception
Traceback (most recent call last):
  File "/home/runner/PDS-RAG/.pythonlibs/lib/python3.10/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 535, in _run_script
    exec(code, module.__dict__)
  File "/home/runner/PDS-RAG/pages/1_ClaimCover.py", line 5, in <module>
    from pydantic import BaseModel, Field, ConfigDict
  File "<frozen importlib._bootstrap>", line 1075, in _handle_fromlist
  File "/home/runner/PDS-RAG/.pythonlibs/lib/python3.10/site-packages/pydantic/__init__.py", line 383, in __getattr__
    module = import_module(module_name, package=package)
  File "/nix/store/xf54733x4chbawkh1qvy9i1i4mlscy1c-python3-3.10.11/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1024, in _find_and_load
  File "<frozen importlib._bootstrap>", line 170, in __enter__
  File "<frozen importlib._bootstrap>", line 185, in _get_module_lock
NameError: name '_imp' is not defined