Import Local module while using AppTest - Error

I’m running my app locally, and want to set up a simple testing framework with the inbuilt AppTest. It is a multipage app and I’m using Poetry.

Structure:

Streamlit version: 1.28.2
Python version: 3.10.12

My entry file is the 0_X_Home.py (where X is the emoji). I’m importing my local utils package for added functionality. Here, I’m using the set_state module:

image

The app works fine locally using streamlit run 0_X_Home.py, but when I create the test file test_home.py and use AppTest.from_file and point to the entry file, it gives the following error when I run pytest.

2023-11-20 11:57:23.824 Uncaught app exception
Traceback (most recent call last):
  File "/home/ezra/.cache/pypoetry/virtualenvs/az-streamlit-qhVRzfSP-py3.10/lib/python3.10/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 534, in _run_script
    exec(code, module.__dict__)
  File "/home/ezra/az-streamlit/az_streamlit/0_🏠_Home.py", line 2, in <module>
    from utils import set_state
ModuleNotFoundError: No module named 'utils'

I’ve checked out some example apps but can’t figure out where I’m going wrong.

Run your tests from the same directory where you are running the application.

Thanks for taking a look. I’ve tried a few different setups.

image

From ~/az-streamlit/az_streamlit I run either poetry run pytest or poetry run pytest test_home.py, in both cases I get the same error:

=============================================================================== FAILURES ================================================================================
__________________________________________________________________________ test_set_study_code __________________________________________________________________________

    def test_set_study_code():
        at = AppTest.from_file("0_🏠_Home.py").run()
>       assert not at.exception
E       assert not ElementList(_list=[Exception(message="No module named 'utils'", stack_trace=['File "/home/ezra/.cache/pypoetry/virtual...)', 'File "/home/ezra/az-streamlit/az_streamlit/0_🏠_Home.py", line 2, in <module>\n    from utils import set_state'])])
E        +  where ElementList(_list=[Exception(message="No module named 'utils'", stack_trace=['File "/home/ezra/.cache/pypoetry/virtual...)', 'File "/home/ezra/az-streamlit/az_streamlit/0_🏠_Home.py", line 2, in <module>\n    from utils import set_state'])]) = AppTest(_script_path='0_🏠_Home.py', default_timeout=3, session_state={$$STREAMLIT_INTERNAL_KEY_SCRIPT_RUN_WITHOUT_ERRO... 2, in <module>\n    from utils import set_state'])}), 1: SpecialBlock(type='sidebar'), 2: SpecialBlock(type='event')}).exception

test_home.py:5: AssertionError
------------------------------------------------------------------------- Captured stderr call --------------------------------------------------------------------------
2023-11-20 12:26:53.249 Uncaught app exception
Traceback (most recent call last):
  File "/home/ezra/.cache/pypoetry/virtualenvs/az-streamlit-qhVRzfSP-py3.10/lib/python3.10/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 534, in _run_script
    exec(code, module.__dict__)
  File "/home/ezra/az-streamlit/az_streamlit/0_🏠_Home.py", line 2, in <module>
    from utils import set_state
ModuleNotFoundError: No module named 'utils'
======================================================================== short test summary info ========================================================================
FAILED test_home.py::test_set_study_code - assert not ElementList(_list=[Exception(message="No module named 'utils'", stack_trace=['File "/home/ezra/.cache/pypoetry/virtual...)', 'File "/home/ezra/az-streaml...

FYI

'''test_home.py contents'''
from streamlit.testing.v1 import AppTest

def test_set_study_code():
    at = AppTest.from_file("0_🏠_Home.py").run()
    assert not at.exception

I was also affected by this and found a solution.

Run pytest with python and as a module:

poetry run python -m pytest

In fairness, I found this solution here Getting "Module not found error" when attempting to import user package · Issue #1780 · streamlit/streamlit · GitHub

Amazing - fixed it. Thank you!

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