FileNotFound errors on deployment with Streamlit

I tried deploying a model with streamlit sometime in the past month. After building and testing locally, I found it works perfectly fine.

I went ahead with pushing to GitHub and opening a Streamlit account to host the model with. Sadly I’ve only gotten errors. The problem seems to be that Streamlit (or GitHub ?) can’t locate the files with the paths provided. E.g. It can’t locate the dataset file, or the saved model. I get FileNotFoundError or ModuleNotFoundError.

I attempted moving all files to the root of the project, but with same results, also tried getting the file from via url in the github repo, but that returns a webpage instead of the intended file. Various tricks to building the filepath (os, Posix, etc) didn’t work either. Nothing on forums seem to work.

Could someone spare some time to look at it and suggest what I might be missing?
Here’s the repo:

I would appreciate.

Hey @maradeben, can you share an example of the path you’re providing to the file? Or point us to a specific line in the app?

Thanks for your response. Here is the traceback I get:

Traceback (most recent call last):

  File "/home/appuser/venv/lib/python3.9/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 565, in _run_script

    exec(code, module.__dict__)

  File "/app/africa-financial-inclusion/deployment/app.py", line 6, in <module>

    from prediction import predict

  File "/app/africa-financial-inclusion/deployment/prediction.py", line 8, in <module>

    with open(path_to_model, 'rb') as file:

FileNotFoundError: [Errno 2] No such file or directory: '../models/final_model.pkl'

I have a structure like so:
root-|
deployment-|
app.py
predictions.py
utils.py
models-|
…model.pkl
data-|
…data.csv

I’m having no trouble importing modules between the .py files in deployment. But I have to load the trained model and data flies from their respective folders, and that’s where the error comes from. E.g. I try loading the a model from predictions.py by providing a path like:
…/models/mymodel.pkl
but I get the file not found error.
The same goes for the files in the data directory

I recently attempted downloading the file from GitHub with wget to avoid working with saved files/filepaths. Again, I got it to work locally, but on deployment, I get a modulenotfound error with wget. Adding a requirements.txt gives ‘Error installing requirements.’

@maradeben

This error occur due to path problem. I mean it can’t locate file from given path.
You need to append parent directory path.

Please install path module also add it to requirements.txt

and make following changes in code.

from utils import wrangle
import pickle
import sys
import path

dir = path.Path(__file__).abspath()
sys.append.path(dir.parent.parent)

# load model
path_to_model = './models/final_model.pkl'


with open(path_to_model, 'rb') as file:
    model = pickle.load(file)

def predict(data):
    """Make predictions"""
    processed = wrangle(data, test=True)
    result = model.predict(processed)
    output = ''
    if result == 0:
        output = "Not Likely To Have Bank Account"
    else:
        output = "Likely To Have a Bank Account"
    return output

try this.

Processing /opt/conda/conda-bld/bottleneck_1657175564434/work

ERROR: Could not install packages due to an OSError: [Errno 2] No such file or directory: '/opt/conda/conda-bld/bottleneck_1657175564434/work'


WARNING: You are using pip version 22.0.3; however, version 23.0.1 is available.

You should consider upgrading via the '/home/appuser/venv/bin/python -m pip install --upgrade pip' command.

Checking if Streamlit is installed

Thank you.

I have implemented the suggestions. Still it works fine locally, but I get this on deployment. I’ve also updated the pip version to v23 but it still shows this.

What could I be doing wrong this time?

Your requirements.txt file is cluttered with unnecessary and not-installable packages. Cleanup the file and retry.

Oh, right. Thank you so much.
I removed those “@ [path]” after the packages. I noticed they had the /work at the end, which seemed to be the same thing in the traceback.

It all works fine now.

I appreciate everyone for their contributions.

Finally works after almost a month of frustrations.:grin:

Hey I think you might have made a little typo here? I just thought to point it out for the next person.

This line:

sys.append.path(dir.parent.parent)

should be

sys.path.append(dir.parent.parent)

Thanks once again for your help.

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