Streamlit fails to import package included in requirements.txt

Hi,

I want to build an app that uses the package ‘SimpleITK’. This is a normal pip package https://pypi.org/project/SimpleITK/ and normally easy to install / makes no problems.

The package is included in my requirements.txt, which looks like this:

numpy
scikit-image
SimpleITK

However, when I execute my code with streamlit run streamlit.py, I get the following error:

2022-06-07 22:04:16.959 Uncaught app exception
Traceback (most recent call last):
  File "/home/k539i/.local/lib/python3.8/site-packages/streamlit/scriptrunner/script_runner.py", line 554, in _run_script
    exec(code, module.__dict__)
  File "/home/k539i/Documents/projects/pytorch.3D_U-Net_baseline/streamlit.py", line 39, in <module>
    prediction, prediction_name = inference()
  File "/home/k539i/Documents/projects/pytorch.3D_U-Net_baseline/streamlit.py", line 9, in inference
    import SimpleITK as sitk
ModuleNotFoundError: No module named 'SimpleITK'

My streamlit.py:

import streamlit as st
import os
import time
import numpy as np
from skimage import transform as ski_transform
import SimpleITK as sitk


def inference():
    my_bar = st.progress(0)
    for percent_complete in range(100):
        time.sleep(0.1)
        my_bar.progress(percent_complete + 1)
    prediction = np.zeros((100, 100, 100), dtype=np.uint8)
    prediction = sitk.GetImageFromArray(prediction)
    spacing = (0.1, 0.1, 0.1)
    prediction.SetSpacing(spacing)
    # prediction = None
    return prediction, "test"

st.title('Mineral particle segmentation')

# Initialization
if 'key' not in st.session_state:
    st.session_state['key'] = 'Init'

# Upload
uploaded_file = st.file_uploader("Upload a an image", type=['.nii.gz'])
if uploaded_file is not None:
    file_details = {"FileName": uploaded_file.name, "FileType": uploaded_file.type}
    # st.write(file_details)
    with open(os.path.join("streamlit_uploaded", uploaded_file.name), "wb") as f:
        f.write(uploaded_file.getbuffer())
    st.success("Saved image")
    st.session_state['key'] = 'Uploaded'

if st.session_state['key'] == 'Uploaded':
    st.session_state['key'] = 'Inference'
    prediction, prediction_name = inference()

    st.download_button(
         label="Download prediction",
         data=prediction,
         file_name='prediction_name.nii.gz',
         mime='file',
     )

It seems SimpleITK could not be installed even though it is in the requirements.txt. Any ideas?

Best,
Karol

Hi Karol.

Before running the Streamlit app, are you ensuring that all requirements are installed with:

pip install -r requirements.txt
1 Like

Hi,

ah, thanks! Yes, that worked.

Best,
Karol

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