Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Console script returning RuntimeError #8606

Closed
4 tasks done
gabrieltempass opened this issue May 3, 2024 · 8 comments · Fixed by #8620
Closed
4 tasks done

Console script returning RuntimeError #8606

gabrieltempass opened this issue May 3, 2024 · 8 comments · Fixed by #8620
Assignees
Labels
feature:cli Related to the command line interface status:confirmed Bug has been confirmed by the Streamlit team status:in-progress We're on it! type:bug Something isn't working

Comments

@gabrieltempass
Copy link

Checklist

  • I have searched the existing issues for similar issues.
  • I added a very descriptive title to this issue.
  • 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?

  • 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 gabrieltempass added status:needs-triage Has not been triaged by the Streamlit team type:bug Something isn't working labels May 3, 2024
Copy link

github-actions bot commented May 3, 2024

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

@raethlein
Copy link
Collaborator

raethlein commented May 3, 2024

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.

@kaizen63
Copy link

kaizen63 commented May 4, 2024

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

@FloTeu
Copy link

FloTeu commented May 4, 2024

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!

@lukasmasuch lukasmasuch added the feature:cli Related to the command line interface label May 4, 2024
@hansthen
Copy link

hansthen commented May 5, 2024

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

@raethlein
Copy link
Collaborator

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

@raethlein raethlein added status:confirmed Bug has been confirmed by the Streamlit team status:in-progress We're on it! and removed status:needs-triage Has not been triaged by the Streamlit team labels May 5, 2024
raethlein added a commit that referenced this issue May 6, 2024
## Describe your changes

Closes #8606

The PR allows custom component modules to be executed standalone by not
registering them with Streamlit at all, which makes sense since
Streamlit is not started.

It's a cherry-pick of this broader PR
#8610, which also adds tests.
However, we want to discuss our testing strategy first, so we decouple
it from adding the fix (see for example also this PR
#8616).

## GitHub Issue Link (if applicable)

- #8606
- NathanChen198/streamlit-rsa-auth-ui#1
- randyzwitch/streamlit-folium#186

## Testing Plan

- E2E Tests: tests to ensure this behavior will come in a separate PR,
see #8616 and
#8610

---

**Contribution License Agreement**

By submitting this pull request you agree that all contributions to this
project are made under the Apache 2.0 license.
@raethlein
Copy link
Collaborator

raethlein commented May 6, 2024

We have merged #8620 that should fix this issue, so it should land in streamlit-nightly and latest in 1.35 🙂

@gabrieltempass
Copy link
Author

Awesome, thanks!

XuanYang-cn added a commit to XuanYang-cn/VectorDBBench that referenced this issue May 15, 2024
alwayslove2013 pushed a commit to zilliztech/VectorDBBench that referenced this issue May 16, 2024
benjamin-awd pushed a commit to benjamin-awd/streamlit that referenced this issue Sep 29, 2024
## Describe your changes

Closes streamlit#8606

The PR allows custom component modules to be executed standalone by not
registering them with Streamlit at all, which makes sense since
Streamlit is not started.

It's a cherry-pick of this broader PR
streamlit#8610, which also adds tests.
However, we want to discuss our testing strategy first, so we decouple
it from adding the fix (see for example also this PR
streamlit#8616).

## GitHub Issue Link (if applicable)

- streamlit#8606
- NathanChen198/streamlit-rsa-auth-ui#1
- randyzwitch/streamlit-folium#186

## Testing Plan

- E2E Tests: tests to ensure this behavior will come in a separate PR,
see streamlit#8616 and
streamlit#8610

---

**Contribution License Agreement**

By submitting this pull request you agree that all contributions to this
project are made under the Apache 2.0 license.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature:cli Related to the command line interface status:confirmed Bug has been confirmed by the Streamlit team status:in-progress We're on it! type:bug Something isn't working
Projects
None yet
6 participants