Change run command on streamlit cloud

I need to run my app as a module with python -m streamlit run folder/app,py so that importing works. Without this, having from folder import func inside my streamlit app gives a ModuleNotFound error

1 Like

Hi @batman,

Let me break down your questions into 2 and provide answers below.

  1. I need to run my app as a module with python -m streamlit run folder/app,py so that importing works.

To run Streamlit apps in the terminal, you need to run the following:

streamlit run app.py
  1. Without this, having from folder import func inside my streamlit app gives a ModuleNotFound error
    In order to make use of custom functions inside your Streamlit apps, you can do the following:

Step 1. Create a utilities.py file (can also be named anything you like) that stores your custom functions.

Step 2. From within your app.py file make sure to use the following import statements to use your custom functions:

import streamlit as st
from utilities import your_custom_function

Oh hey @dataprofessor! I’ve really enjoyed your tutorials :smiley:

My utilities are already in a different file but it’s within a folder. So my old directory structure had utilities.py in root but the new structure it is now tools/utilities.py

The problem I have is that when running streamlit I get a module not found error, but when I run as a module with python -m streamlit run devapp/app.py it works as expected

1 Like

Hey glad to hear that.

For the import statement inside your app.py file, can you try from tools.utilities import your_custom_function

1 Like

Yes, that’s how I am currently importing :smiley:

Using this, running with streamlit run gives me a module not found error. The import works when using python -m streamlit run

1 Like

I think it may be the relative path that may be the cause of the problem. It seems that your app is at devapp/app.py while your custom functions are inside tools/utilities.py’. Could you try moving the latter to devapp/tools/utilities.py`.

Yes; running as a module with -m allows the relative imports to work :smiley:

Moving tools into devapp would be ideal but I don’t think that’ll work for my particular problem (open to suggestions!) since I did this to run multiple Streamlit app from one repo

A simplified look at my directory structure:

tools/
- utils.py
- utils2.py

devapp/
- pages/
- app.py

publicapp/
- app.py

Having tools inside devapp means I’d have to duplicate tools in both devapp and publicapp

Do you think there’s a better way to do this? Or maybe Streamlit cloud is not the way to go for this

1 Like

Hi, thanks for the detailed account of the directory structure.

Perhaps, you can also try exporting your module’s path to system path as detailed in How can I make Streamlit watch for changes in other modules I'm importing in my app? - Streamlit Docs

Although the above may help address the local use of Streamlit as for using via Streamlit Cloud, it supports hosting a single page app or a multipage app. From the directory structure, it seems to have both single page app (publicapp) and the multipage app (devapp). This might be best hosted on a custom VPS.

Another possibility could be to merge both publicapp and devapp so that perhaps devapp could reside as part of the multipage app.