Check that Firebase app is already initialized in python
I am getting the following 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.
How to check if default firebase app is already initialized or not in python?
source to share
The best way is to control the application workflow so that initialization is called only once. But of course, inefficient code is good too, so here's what you can do to avoid this error:
import firebase_admin
from firebase_admin import credentials
if (not len(firebase_admin._apps)):
cred = credentials.Certificate('path/to/serviceAccountKey.json')
default_app = firebase_admin.initialize_app(cred)
source to share
Initialize application in constructor
cred = credentials.Certificate('/path/to/serviceAccountKey.json') firebase_admin.initialize_app(cred)
then in your method you call
firebase_admin.get_app()
https://firebase.google.com/docs/reference/admin/python/firebase_admin
source to share
I found the following to work for me.
For the default app:
import firebase_admin
from firebase_admin import credentials
if firebase_admin._DEFAULT_APP_NAME in firebase_admin._apps:
# do something.
I used it this way with a named app:
import firebase_admin
from firebase_admin import credentials
if 'my_app_name' not in firebase_admin._apps:
cred = credentials.Certificate('path/to/serviceAccountKey.json')
firebase_admin.initialize_app(cred, {
'databaseURL': 'https://{}.firebaseio.com'.format(project_id),
'storageBucket': '{}.appspot.com'.format(project_id)}, name='my_app_name')
source to share