You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Pyrebase4开发登录界面遇AttributeError问题求助

Pyrebase4登录/创建用户报错:AttributeError: 'function' object has no attribute 'xxx'

问题现象

使用Pyrebase4实现登录界面时,调用登录和创建用户方法均失败,报错如下:

AttributeError: 'function' object has no attribute 'sign_in_with_email_and_password'
AttributeError: 'function' object has no attribute 'create_user_with_email_and_password'

使用的版本信息:

  • Python:3.10.4
  • Kivy:2.1.0
  • KivyMD:0.104.2
  • Pyrebase4:4.5.0

问题原因

代码里错误直接把firebase.auth赋值给了auth变量,但firebase.auth是一个方法,必须调用它才能返回Auth实例。直接赋值会让auth变成函数对象,自然没有sign_in_with_email_and_password这类实例方法。

解决方案

把auth = firebase.auth改成auth = firebase.auth(),通过调用方法获取正确的Auth实例。

修改后的代码(main.py)

from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
import pyrebase

class MainApp(MDApp):
    
    def build(self):
        return Builder.load_file('main.kv')
    
    firebaseConfig = {
        '''write your config here'''
    }
    firebase = pyrebase.initialize_app(firebaseConfig)
    auth = firebase.auth()  # 此处添加括号调用方法
    
    def login(self):
        email = self.root.get_screen("test1win").ids.user_mail.text
        password = self.root.get_screen("test1win").ids.password.text
        
        login = self.auth.sign_in_with_email_and_password(email, password)
        self.root.current = "test2win"
        print("Login Success")

class WindowManager(ScreenManager):
    pass

class TestWindow1(Screen):
    pass

class TestWindow2(Screen):
    pass

if __name__ == "__main__":
    MainApp().run()

补充说明

如果要实现创建用户功能,同样用修改后的auth实例调用create_user_with_email_and_password方法即可,示例代码:

def create_user(self):
    email = "example@test.com"
    password = "123456"
    user = self.auth.create_user_with_email_and_password(email, password)

内容的提问来源于stack exchange,提问作者mkfmtr

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 06:06:06