Streamlit error FileNotFoundError

Summary

Hey all!

I’m new to streamlit and I’m trying to run a web app using it. My problem is:

FileNotFoundError: [Errno 2] No such file or directory: ‘data/DadosCopaDoMundoQatar2022.xlsx’

but this file exists! I already searched the forum for this problem, and found that the solution was to include in requirements.txt the openpyxl, but that I already did, and the problem persists. I also tried different paths to the file, but unsuccessful. My code is just

import streamlit as st
from sklearn.preprocessing import MinMaxScaler 
import pandas as pd
import numpy as np
from scipy.stats import poisson

st.title('FIFA World Cup Qatar 2022™ A.I.')

teams = pd.read_excel('data/DadosCopaDoMundoQatar2022.xlsx',
                        sheet_name ='selecoes',
                        names = ['Teams_BR',
                                 'Group',
                                 'GroupNumber',
                                 'Team',
                                 'FIFAsRankPosition',
                                 'MarketValue',
                                 'FIFAsRankingPoints',
                                 'Confederation',
                                 'WorldCups',
                                 'SpotlightPlayer',
                                 'PictureOfPlayer',
                                 'FlagsLinkSmall',
                                 'FlagsLinksBig'],
                        index_col = 0)

requirements.txt:

streamlit==1.14.0
pandas==1.5.1
numpy==1.23.0
scipy==1.9.3
openpyxl==3.0.7 #or 3.0.10 (error occurs for both)
scikit-learn

All the files are in my GitHub: Data-Science-projects/project_6 at master · SirTales/Data-Science-projects · GitHub
The data to be read is in ‘project_6/data/’, and in my experience there should be no error there.
I already tried:

pd.read_excel('https://github.com/SirTales/Data-Science-projects/tree/master/project_6/data/DadosCopaDoMundoQatar2022.xlsx')

pd.read_excel('Data-Science-projects/tree/master/project_6/data/DadosCopaDoMundoQatar2022.xlsx')

and even moving the .xlsx to the same folder of web_app.py (streamlit deploy) such as
pd.read_excel(‘DadosCopaDoMundoQatar2022.xlsx’) but all failed.

Thanks, in advance, for your help. And if anything else is necessary, just ask for, please!
Some infos:

  • Streamlit version: 1.15.2
  • Python version: 3.9.7
  • Ubuntu 22

Links

Hey @SirTales, have you tried using cwd = os.getcwd() to get the current path, and then specifying the full path to the file?

1 Like

Thank you, Caroline! I was able to find the correct path for my data. It was under /app…

1 Like

Ola Sir Tales, diga como vc conseguiu resolver, explicando, estou com esse mesmo problema

Oi Fabiano!
Eu resolvi seguindo o que a colega falou. Inseri em meu streamlit.py as seguintes linhas:

import os
path_to_find = os.listdir()
st.title(path_to_find)

Isso me mostrou o caminho que estavam meus dados. No meu caso, o que o comando retornou foi uma lista com os diretórios de /app/data-science-projects/ criado pelo streamlit ao rodar meu script do GitHub. Meus dados estavam em …/project_6/data/. O comando que ela sugeriu, cwd = os.getcwd() serviu para me mostrar que eu estava em uma pasta diferente da que achava que estava.

By opening a file with the name “filename.ext” using the open() function, you are using a relative path to indicate that the file is located in the current working directory.

file = open('filename.ext') //relative path

In the code snippet above, only the name of the file is provided to the open() function, which is a relative path. If the file with the given name is not present in the working directory, it will raise a “FileNotFoundError: [Errno 2] No such file or directory” error. In such cases, using the exact or absolute path to the file can resolve the issue.

file = open(r'C:\path\to\your\filename.ext') //absolute path

The above code uses an absolute path, which contains all the information needed to locate the file, including the root directory and any subdirectories.

If the user does not provide the full path to the file (which, on Unix systems, must start with a slash), the file path in Python is interpreted relative to the current working directory. By default, the current working directory is the directory from which you started the Python program. However, in order for this to work, the directory that contains the Python executable must be included in the system’s PATH environment variable, which specifies directories that are automatically searched for executables when a command is entered. Regardless, if your Python script and input file are located in different directories, you must either specify a relative path between them or use an absolute path for one of them.