Home > Blockchain >  python unable to find file in same directory
python unable to find file in same directory

Time:03-15

I have built a python flask app that connects to Cloud Firestore, performs a search query, and creates graphs based on what it finds. since this is in python, I have used the firebase admin SDK to do this, which is a JSON file containing the api key. i have followed exactly what the documentation says to do to read the admin SDK. this was working completely fine on the two apps that use the key until I opened the project this morning and it did not work.

import firebase_admin
from firebase_admin import credentials

cred = credentials.Certificate("firebase-SDK.json")
firebase_admin.initialize_app(cred)

running the above code results in

FileNotFoundError: [Errno 2] No such file or directory: 'firebase-SDK.json'

file structure is as follows (apologies if this is not very readable, I hope you can understand it):

> graph-localhost
    > static
        (three images the graph function makes)
    > templates
        index.html
    > app.py (flask app)
    > firebase-SDK.json (this is not the real name of the file, but for privacy I do not want to give it out)
    > grapher.py (this is what connects to the firebase-SDK file.)

I've triple checked and the real file name is spelt correctly and that the path is correct. I even downloaded a new firebase SDK file to see if that was the problem. I have also made sure that firebase-admin is completely up to date, though I doubt this is an issue with firebase. this worked perfectly fine not two days ago and it's the only thing holding me back from completing this project. this issue has also affected the other part of the project, which was created first using the same method, worked perfectly fine, and now does not. I have read here that this can be an issue on Windows, but I am on macOS Big Sur. I have restarted my computer and updated VScode, also.

thanks for any and all help you can give.

CodePudding user response:

That is very strange that it was working previously and has now begun to show the error. Usually the file not found error is due to the path being incorrect. But as you have shown it looks correct.

One thing that could potentially be causing the error is that the file that contains the code you showed is being called from a directory other than the parent directory. If this is the case then the following may fix the issue:

import firebase_admin
from firebase_admin import credentials
import os

file_path = os.path.join(os.path.dirname(os.path.abspath(__file__)),"firebase-SDK.json")
cred = credentials.Certificate(file_path)
firebase_admin.initialize_app(cred)

To get more information, you could try and debug where the function is being called from by adding this print line prior to your credentials line:

print(os.getcwd())

CodePudding user response:

Did you try it with "./firebase-SDK.json" ?

  • Related