0
FileNotFoundError: [Errno 2] No such file or directory: 'static/data/cleanHeart.csv

I receive this error when attempting to access my csv file in my models.py file within a model named Patient and a method named perform_prediction. My intent is to, with the use of some attributes available within Patient, to generate a prediction with the help of the data.

import pandas as pd

def perform_prediction(self):
    main_df = pd.read_csv('static/data/cleanHeart.csv')
    regu = linReg.fit(main_df[['Age','Sex', 'ChestPainType', 'RestingBP', 'Cholesterol', 'FastingBS', 'RestingECG', 'MaxHR', 'ExerciseAngina', 'Oldpeak', 'ST_Slope']],main_df['HeartDisease'])
    tester = regu.predict([[65,0,3,140,306,1,0,87,1,1.5,0]])
    return tester

To ensure my relative path is correct, I created an acceptablepath.py in the exact same location with the method code, and was successfully able to access cleanHeart.csv.

I am calling this function from a template. To ensure that was not the issue, I created a simple ping_me method which has no problems returning to the template.

def ping_me(self):
    return 'ping!'

Is this issue related to the settings.py in anyway? I even placed the cleanHeart.csv in the same directory with models.py and I still am unable to access it.

There are no pending migrations.

Park
  • 2,446
  • 1
  • 16
  • 25
  • Can you please post what your `STATIC_URL` and `STATIC_ROOT` is set to in *settings.py*. – Lzak Feb 15 '22 at 18:37
  • "I created an acceptablepath.py in the exact same location with the method code, and was successfully able to access cleanHeart.csv.": how do you mean? Do you mean you ran `acceptablepath.py` as a script in that directory? Or in another way (what way)? – 9769953 Feb 15 '22 at 18:46

1 Answers1

1

Relative paths such as 'static/data/cleanHeart.csv' are relative to your process's working directory, which may or may not be where manage.py is. (A stray os.chdir() call anywhere in your project will also trash that assumption.)

If your code is in, say, myapp/predictions.py, then put cleanHeart.csv next to that file, i.e. myapp/cleanHeart.csv, and change things to

import pandas as pd
import os

csv_filename = os.path.join(os.path.dirname(__file__), 'cleanHeart.csv')

def perform_prediction():
    main_df = pd.read_csv(csv_filename)
    # ... etc...

to form the path to the data file based on the module's filename.

AKX
  • 152,115
  • 15
  • 115
  • 172