I want to deploy an app with custom python modules that are stored in the repo and imported in the pages definitions of the App.
Steps to reproduce
Please have a look at this dummy repo I set up for this question: GitHub
Setup Conda environment
Add local directory containing python function via conda develop ./scripts
Deploy App locally → Works
→ Deploy App in Cloud → Fails
Expected behavior:
Executing start_app.sh and running the App locally shows that it works. But when I deploy the App in the community cloud I wish it would also find the custom module and not throw an error. How can I run commands such as conda develop ./scripts during the deployment phase?
Actual behavior:
ModuleNotFoundError due to the fact that I can’t tell the App to run conda develop ./scripts
Error in CommunityCloud
[10:59:37] 🐍 Python dependencies were installed from /app/dummy_app/requirements.txt using pip.
Check if streamlit is installed
Streamlit is already installed
[10:59:38] 📦 Processed dependencies!
Collecting usage statistics. To deactivate, set browser.gatherUsageStats to False.
[11:00:50] 🐙 Pulling code changes from Github...
[11:00:51] 📦 Processing dependencies...
[11:00:51] 📦 Processed dependencies!
[11:00:52] 🔄 Updated app!
2023-02-10 11:01:57.484 Uncaught app exception
Traceback (most recent call last):
File "/home/appuser/venv/lib/python3.10/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 565, in _run_script
exec(code, module.__dict__)
File "/app/dummy_app/scripts/app/main_page.py", line 4, in <module>
from helpers.return_random_number import return_some_number
ModuleNotFoundError: No module named 'helpers'
2023-02-10 11:08:05.974 Uncaught app exception
Traceback (most recent call last):
File "/home/appuser/venv/lib/python3.10/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 565, in _run_script
exec(code, module.__dict__)
File "/app/dummy_app/scripts/app/main_page.py", line 4, in <module>
from helpers.return_random_number import return_some_number
ModuleNotFoundError: No module named 'helpers'
Debug info
Streamlit version:1.17.0
Python version:3.10.8
Using Conda
Requirements file
Links
Link to your GitHub repo: See above
Additional information
If needed, add any other context about the problem here.
I just tried your suggestion but it also doesn’t work. In any case it would be great if I could leave the folder structure in that way since I want to keep my scripts and app separated.
Here is the error after trying the suggestion:
[11:46:44] 🐍 Python dependencies were installed from /app/dummy_app/requirements.txt using pip.
Check if streamlit is installed
Streamlit is already installed
[11:46:45] 📦 Processed dependencies!
Collecting usage statistics. To deactivate, set browser.gatherUsageStats to False.
2023-02-10 11:46:54.669 Uncaught app exception
Traceback (most recent call last):
File "/home/appuser/venv/lib/python3.10/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 565, in _run_script
exec(code, module.__dict__)
File "scripts/app/main_page.py", line 4, in <module>
from helpers.return_random_number import return_some_number
ModuleNotFoundError: No module named 'helpers'
2023-02-10 11:52:08.854 Uncaught app exception
Traceback (most recent call last):
File "/home/appuser/venv/lib/python3.10/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 565, in _run_script
exec(code, module.__dict__)
File "/app/dummy_app/scripts/app/main_page.py", line 4, in <module>
from helpers.return_random_number import return_some_number
ModuleNotFoundError: No module named 'helpers'
I branched from Move helpers inside app to test and deployed in streamlit cloud. TL;DR: Importing helpers from the main page worked, but switching pages didn’t.
The main page loaded and I was shown a randon number. But switching pages got me a popup with the message [Errno 2] No such file or directory: 'pages/page_2.py' and nothing worked from that point on. I deleted the st_pages specific code to rely on the builtin pages feature and redeployed the app but it did exactly the same. I had no problems running the app in my computer (Windows 10).
Apparently streamlit cloud doesn’t like something in your folder structure, but I can’t put a finger on it.
I am also trying to deploy a app with 4 custom files. So my requirements.txt, looks like this:
matplotlib==3.6.3
numpy==1.23.5
pandas==1.5.3
seaborn==0.12.2
streamlit==1.20.0
./predict.py
./explore.py
./about_me.py
./home.py
./img.jpg
however, I am getting error when installing these custom files. All the files are present in my repository root folder itself.
Can you suggest some ideas on what could be wrong here
Actually I was able to deploy the app when I placed all the function in the app.py, however, that doesn’t make the code clean. For now, I deployed it. But you will find all the files with the functions in that repo.
The files i wish to add are: home.py, explore.py, predict.py, about.py.
Each of these functions are actually individual sections for my app, that way I can add more options while keeping the app.py clean.
The error message was: Could not find predict.py, home.py and so on. I even imported them in app.py
Thing is when I tested the app on my local, it is running, but during deployment, its causing the error.
Thank you for the help in advance.
Yes, during deployment, I got that error message itself, maybe, because I was only importing a function from my other files rather than the whole file, like
from predict import predict_page from home import home_page
These lines gave me the errors.
So, what I did, I included all the functions in app.py (the one which is now on my repo) and the app deployed.
what I actually want is for the app.py to be simple and clean and export libraries from other files.
Do you think this will work for the custom modules rather than just calling a single function?
import home import predict as pd
and then a simple call
for example: results = pd.predict_page()