Search code examples
pythonfirebasefirebase-admin

relative path not work with credentials.Certificate()


I use Firebase in my python project. To initialize the credential.Certificate() I want to use a relative path like so:

cred_path = 'jsonfile.json'
cred_path = os.path.join(os.path.dirname(__file__),cred_path)

the output is

C:\Users\testuser\Desktop\Rewrite\jsonfile.json

if I pass the path to the firebase method and convert it to an .exe with pyinstaller I get an error.

 cred = credentials.Certificate(m)
firebase_admin.initialize_app(cred, {
    'databaseURL' : 'databaseurl.com'
})

Traceback (most recent call last): File "rewriteword.py", line 24, in File "firebase_admin\credentials.py", line 82, in init FileNotFoundError: [Errno 2] No such file or directory: 'jsonfile.json' [18292] Failed to execute script testscript

If I am using the absolute path it works perfectly with no error. Why does this happen?


Solution

  • Fixed it. I had to add the .json data to pyinstaller.

    cred_path = 'jsonfile.json'
    bundle_dir = getattr(sys, '_MEIPASS', path.abspath(path.dirname(__file__)))
    path_to_dat = path.abspath(path.join(bundle_dir, cred_path))
    
    pyinstaller --onefile --add-data "jsonfile.json;." rewriteword.py