Search code examples
pythonfirebasefirebase-admin

Why I get this error on python firebase-admin initialize_app()?


when I was trying to connect google firebase real time database, I got this error:

ValueError: The default Firebase app already exists. This means you called 
initialize_app() more than once without providing an app name as the second argument. In 
most cases you only need to call initialize_app() once. But if you do want to initialize 
multiple apps, pass a second argument to initialize_app() to give each app a unique name.

Here is my code:

import firebase_admin
from firebase_admin import credentials
from firebase_admin import db

cred = credentials.Certificate('firebase-sdk.json')

firebase_admin.initialize_app(cred, {
'databaseURL': 'https://test-139a6-default-rtdb.firebaseio.com/'
})

Solution

  • You only need to initialize (create) the app once. When you have created the app, use get_app instead:

    # The default app's name is "[DEFAULT]"
    firebase_admin.get_app(name='[DEFAULT]')