Unable to use sketch with Streamlit


Hi Guys. I am trying to use sketch library (sketch · PyPI) in a streamlit app but I am not able to run the app if I am importing sketch. The problem comes the moment I try to import the sketch library itself. To install sketch, you could run this command “pip install sketch”. I am able to use sketch in normal python applications and with jupyter notebook but not with streamlit.

Steps to reproduce

Code snippet:

import streamlit as st
import sketch

Expected behavior:

You should be able to run the streamlit application saying hello.

Actual behavior:
2023-03-29 13:24:23.820 Uncaught app exception
Traceback (most recent call last):
File “/Users/rharneja/Library/Python/3.10/lib/python/site-packages/streamlit/runtime/scriptrunner/script_runner.py”, line 565, in _run_script
exec(code, module.dict)
File “/Users/rharneja/Documents/python_workspace/streamlit_sketch.py”, line 2, in
import sketch
File “/Users/rharneja/Library/Python/3.10/lib/python/site-packages/sketch/init.py”, line 2, in
from .pandas_extension import SketchHelper # noqa
File “/Users/rharneja/Library/Python/3.10/lib/python/site-packages/sketch/pandas_extension.py”, line 16, in
import lambdaprompt
File “/Users/rharneja/Library/Python/3.10/lib/python/site-packages/lambdaprompt/init.py”, line 7, in
File “/Users/rharneja/Library/Python/3.10/lib/python/site-packages/nest_asyncio.py”, line 16, in apply
loop = loop or asyncio.get_event_loop()
File “/Users/rharneja/Library/Python/3.10/lib/python/site-packages/nest_asyncio.py”, line 45, in _get_event_loop
loop = events.get_event_loop_policy().get_event_loop()
File “/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/asyncio/events.py”, line 656, in get_event_loop
raise RuntimeError(‘There is no current event loop in thread %r.’
RuntimeError: There is no current event loop in thread ‘ScriptRunner.scriptThread’.

Debug info

  • Streamlit version: Streamlit, version 1.19.0
  • Python version: Python 3.10.1
  • Using PipEnv
  • OS version: Mac OS Monterey V12.6.1
  • Browser version: Google Chrome Version 111.0.5563.110 (Official Build) (x86_64)

Hey @Rakshit_Harneja,

@snehankekre answered the same question on another thread – sharing his answer below.

You’re encountering the error because the library you’re importing (sketch) imports another library (lambdaprompt) that uses async functions. But Streamlit runs in a separate thread that doesn’t have an event loop by default. To make it work, you’ll need to create an event loop and run the async functions inside it.

The lambdaprompt library runs some async code during the import process itself. So you can try creating an event loop before importing the library. Here’s an example:

import asyncio
from contextlib import contextmanager
import streamlit as st

# Create a context manager to run an event loop
def setup_event_loop():
    loop = asyncio.new_event_loop()
        yield loop

# Use the context manager to create an event loop
with setup_event_loop() as loop:
    import sketch

# Now you can use the 'sketch' library in your Streamlit app
st.write("The 'sketch' library has been successfully imported.")


This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.