ModuleNotFoundError: No module named 'main'

The code works perfectly fine on my local. However, when deploying on Streamlit I got caught ModuleNotFoundError. I try different things but still cannot load the modules.

Traceback (most recent call last):

  File "/home/adminuser/venv/lib/python3.11/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 552, in _run_script

    exec(code, module.__dict__)

  File "/mount/src/predictive-app/main/app/app.py", line 14, in <module>

    from predict import make_prediction, preprocess_data

  File "/mount/src/predictive-app/main/app/predict.py", line 5, in <module>

    from main.model.model_loader import load_models

ModuleNotFoundError: No module named 'main'

I also use setuptools still not work.

This is my repo: GitHub - RoadToDev101/predictive-app

Hey @RoadToDev101,

Are you able to share a public version of your GitHub repo (the linked repo is private) so we can try to reproduce the error?

I switched the repo to public. Please feel free to reproduce it.

After some workaround I can get rid of ModuleNotFoundError, but now it cannot find the joblib file

Streamlit Community Cloud runs Python in the root of your repo, so any file paths that you’re specifying in the app may need to be updated when you’re deploying the app vs. running it locally. It looks like someone ran into a super similar issue with a .joblib file in this thread and resolved it by updating the file path.

I think if you switch your model loader to use this:

from pathlib import Path

path = Path(__file__).parent.resolve() # get the path to the folder that the current file is in

It should probably work, as it will then look in the same folder that the model_loader.py is in

2 Likes

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