streamlit icon indicating copy to clipboard operation
streamlit copied to clipboard

Console script returning RuntimeError

Open gabrieltempass opened this issue 2 years ago • 8 comments

Checklist

  • [X] I have searched the existing issues for similar issues.
  • [X] I added a very descriptive title to this issue.
  • [X] I have provided sufficient information below to help reproduce this issue.

Summary

When you create a Python package, you may want to provide a user-friendly CLI, via an entry point console script. Here is an excerpt from setuptools explaining it:

Entry points are a type of metadata that can be exposed by packages on installation. They are a very useful feature of the Python ecosystem, and come specially handy in two scenarios:

  1. The package would like to provide commands to be run at the terminal. This functionality is known as console scripts. The command may also open up a GUI, in which case it is known as a GUI script. An example of a console script is the one provided by the pip package, which allows you to run commands like pip install in the terminal.

A practical example is when you install streamlit, and want to check which version is installed. So you run in the terminal:

streamlit --version

This is a console script.

Console scripts are also very useful for Streamlit Components that are distributed via PyPI. And they used to work until version 1.33.0. However, with the new 1.34.0 release, they no longer do. Now, trying to run a console script from a Streamlit Component returns a RuntimeError.

Reproducible Code Example

No response

Steps To Reproduce

  1. In the terminal, create a Python virtual environment:
python3 -m venv venv
  1. Activate the environment:
. venv/bin/activate
  1. Install a Streamlit Component with a console script, for example Streamlit Navigation Bar:
pip install streamlit-navigation-bar
  1. Run the respective console script, in this case it is:
streamlit-navigation-bar

You get a RuntimeError.

Expected Behavior

This is the expected output from the previous example:

Streamlit Navigation Bar, version 3.1.2

And until Streamlit version 1.33.0, this was the actual output.

Current Behavior

With Streamlit version 1.34.0 installed, this is the output:

Traceback (most recent call last):
  File "/Users/gabrieltempass/Repositories/streamlit-navigation-bar/venv/bin/streamlit-navigation-bar", line 33, in <module>
    sys.exit(load_entry_point('streamlit-navigation-bar', 'console_scripts', 'streamlit-navigation-bar')())
  File "/Users/gabrieltempass/Repositories/streamlit-navigation-bar/venv/bin/streamlit-navigation-bar", line 25, in importlib_load_entry_point
    return next(matches).load()
  File "/Users/gabrieltempass/miniconda3/lib/python3.9/importlib/metadata.py", line 86, in load
    module = import_module(match.group('module'))
  File "/Users/gabrieltempass/miniconda3/lib/python3.9/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 850, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "/Users/gabrieltempass/Repositories/streamlit-navigation-bar/streamlit_navigation_bar/__init__.py", line 9, in <module>
    from streamlit_navigation_bar.match_navbar import MatchNavbar
  File "/Users/gabrieltempass/Repositories/streamlit-navigation-bar/streamlit_navigation_bar/match_navbar.py", line 2, in <module>
    from streamlit_theme import st_theme
  File "/Users/gabrieltempass/Repositories/streamlit-navigation-bar/venv/lib/python3.9/site-packages/streamlit_theme/__init__.py", line 20, in <module>
    _st_theme = components.declare_component(
  File "/Users/gabrieltempass/Repositories/streamlit-navigation-bar/venv/lib/python3.9/site-packages/streamlit/components/v1/component_registry.py", line 87, in declare_component
    get_instance().component_registry.register_component(component)
  File "/Users/gabrieltempass/Repositories/streamlit-navigation-bar/venv/lib/python3.9/site-packages/streamlit/runtime/__init__.py", line 28, in get_instance
    return Runtime.instance()
  File "/Users/gabrieltempass/Repositories/streamlit-navigation-bar/venv/lib/python3.9/site-packages/streamlit/runtime/runtime.py", line 163, in instance
    raise RuntimeError("Runtime hasn't been created!")
RuntimeError: Runtime hasn't been created!

Is this a regression?

  • [X] Yes, this used to work in a previous version.

Debug info

  • Streamlit version: 1.34.0
  • Python version: 3.9
  • Operating System: macOS 14.4.1

Additional Information

No response

gabrieltempass avatar May 03 '24 18:05 gabrieltempass

If this issue affects you, please react with a 👍 (thumbs up emoji) to the initial post.

Your feedback helps us prioritize which bugs to investigate and address first.

Visits

github-actions[bot] avatar May 03 '24 18:05 github-actions[bot]

Acknowledged! This behavior change seems to have been introduced via this PR, since now the Component Registry is loaded from the runtime and is not a singleton anymore. When you run the standalone command, the runtime does not seem to be initialized, resulting in the error you are seeing. I will talk with the team and get back to you with regards to this. I am wondering whether there is a good way to solve this without invoking Streamlit under the hood at all in order to just print the version of the python package.

raethlein avatar May 03 '24 19:05 raethlein

Hello, this is the same issue I raised here: https://github.com/NathanChen198/streamlit-rsa-auth-ui/issues/1 Worked in 1.33 but in 1.34 its broken. Raises a RunTimeError too.

kaizen63 avatar May 04 '24 05:05 kaizen63

I can confirm the same issue as well for the import of custom components.

Reproducable code: Expects custom component streamlit_elements is installed

file: streamlit_bug_report.py

import streamlit as st
# This line raises "RuntimeError: Runtime hasn't been created!" in streamlit>=1.34.0
from streamlit_elements import elements, dashboard, mui, nivo

def app():
    # assume somthing with the streamlit_elements component happens here
    st.write("Hello World")


if __name__ == "__main__":
    app()

runs fine if one starts the app with streamlit run streamlit_bug_report.py

But if the app is started via cli with help of streamlit.web.bootstrap its now raising an error since streamlit version 1.34.0.

file: streamlit_bug_report_cli.py

import streamlit as st
import streamlit.web.bootstrap as st_bootstrap
from streamlit_bug_report import app

def main():
    if st.runtime.exists():
        # The app has been executed with `streamlit run app.py`
        app()
    else:
        # If the file has been executed with python (`python app.py`), the streamlit functionality
        # won't work. This line reruns the app within the streamlit context, as if it has been
        # executed with `streamlit run app.py`.
        # This is necessary when installing this project from a .whl package, since the executable
        # only gets execute by python and not by streamlit.
        st_bootstrap.run(
            __file__,
            is_hello=False,
            args=[],
            flag_options={},
        )


if __name__ == "__main__":
    main()

Running python streamlit_bug_report_cli.py will raise RuntimeError: Runtime hasn't been created!

FloTeu avatar May 04 '24 13:05 FloTeu

The issue also affects automated tests in packages like streamlit-folium, where custom components are tested without actually running Streamlit.

hansthen avatar May 05 '24 08:05 hansthen

Hey y'all, just a quick update that we are going to discuss the issue & potential fix (https://github.com/streamlit/streamlit/pull/8610) tomorrow and I'll keep you posted. Sorry for the inconvenience and thanks for reporting the issue with these details ❤️

raethlein avatar May 05 '24 13:05 raethlein

We have merged https://github.com/streamlit/streamlit/pull/8620 that should fix this issue, so it should land in streamlit-nightly and latest in 1.35 🙂

raethlein avatar May 06 '24 20:05 raethlein

Awesome, thanks!

gabrieltempass avatar May 06 '24 20:05 gabrieltempass